<?php
/**
 * File Path: includes/import/class-mbs-importer-retailer-detector.php
 *
 * MBS_Importer_Retailer_Detector -- Shared retailer detection utility.
 *
 * Converts a buy-link URL or button label string into a canonical MBS
 * retailer_key slug. Used by all competitor WXR importers so the
 * RETAILER_SLUG_MAP lives in exactly one place.
 *
 * Adding a new retailer in a future release: append a keyword => slug
 * entry to RETAILER_SLUG_MAP. Both detect_from_url() and
 * detect_from_label() consume the same map.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Import
 * @since      1.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Importer_Retailer_Detector { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Keyword-to-retailer-slug map.
	 *
	 * Keys are lowercase substrings matched against the URL host or
	 * lowercased label text. First match wins. Anything unmatched
	 * returns an empty string (unknown retailer).
	 *
	 * @since 1.2.0
	 * @var array<string, string>
	 */
	const RETAILER_SLUG_MAP = array(
		'amazon'      => 'amazon',
		'audible'     => 'audible',
		'barnes'      => 'bn',
		'noble'       => 'bn',
		'b&n'         => 'bn',
		'barnesandnoble' => 'bn',
		'apple'       => 'apple',
		'ibook'       => 'apple',
		'itunes'      => 'apple',
		'books.apple' => 'apple',
		'kobo'        => 'kobo',
		'google'      => 'google',
		'bookshop'    => 'bookshop',
		'indiebound'  => 'bookshop',
		'powells'     => 'powells',
		"powell's"    => 'powells',
		'thriftbooks' => 'thriftbooks',
		'libro'       => 'libro',
		'chirp'       => 'chirp',
		'goodreads'   => 'goodreads',
		'storygraph'  => 'storygraph',
		'bookbub'     => 'bookbub',
		'waterstones' => 'waterstones',
		'smashwords'  => 'smashwords',
		'draft2digital' => 'draft2digital',
		'scribd'      => 'scribd',
		'overdrive'   => 'overdrive',
		'hoopla'      => 'hoopla',
	);

	/**
	 * Detect a retailer slug from a buy-link URL.
	 *
	 * Extracts the host from the URL, then tests each map key as a
	 * substring of the lowercased host. First match wins.
	 *
	 * @since  1.2.0
	 * @param  string $url Raw buy-link URL.
	 * @return string      Retailer slug (e.g. 'amazon'), or '' if unrecognized.
	 */
	public static function detect_from_url( string $url ): string {
		if ( '' === $url ) {
			return '';
		}

		$host = strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) );

		foreach ( self::RETAILER_SLUG_MAP as $keyword => $slug ) {
			if ( false !== strpos( $host, $keyword ) ) {
				return $slug;
			}
		}

		return '';
	}

	/**
	 * Detect a retailer slug from a human-readable button label.
	 *
	 * Lowercases the label and tests each map key as a substring.
	 * Intended as a fallback when the URL alone is insufficient
	 * (e.g. redirect URLs, vanity domains, shortened links).
	 * First match wins.
	 *
	 * @since  1.2.0
	 * @param  string $label Button text (e.g. "Buy on Amazon", "Barnes & Noble").
	 * @return string        Retailer slug, or '' if unrecognized.
	 */
	public static function detect_from_label( string $label ): string {
		if ( '' === $label ) {
			return '';
		}

		$lower = strtolower( $label );

		foreach ( self::RETAILER_SLUG_MAP as $keyword => $slug ) {
			if ( false !== strpos( $lower, $keyword ) ) {
				return $slug;
			}
		}

		return '';
	}
}
