<?php
/**
 * Icon library for BW Map Magnet — used for category markers and filter chips.
 * Style: 24x24 monoline, stroke=currentColor, no fill. Tinted by the parent's `color` CSS prop.
 */

defined( 'ABSPATH' ) || exit;

class BW_Map_Magnet_Icons {

	const DEFAULT_KEY = 'pin';

	/**
	 * Library of icon SVG inner-HTML strings (paths, no <svg> wrapper).
	 *
	 * @return array<string,array{label:string,svg:string}>
	 */
	public static function library() {
		static $cache = null;
		if ( null !== $cache ) {
			return $cache;
		}

		$cache = [
			'pin'        => [ 'label' => __( 'Pin', 'bw-map-magnet' ),        'svg' => '<path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0z"/><circle cx="12" cy="10" r="3"/>' ],
			'beach'      => [ 'label' => __( 'Beach / Umbrella', 'bw-map-magnet' ), 'svg' => '<path d="M22 12a10 10 0 0 0-20 0z"/><path d="M12 12v8a2 2 0 0 0 4 0"/><path d="M12 2v2"/>' ],
			'palm'       => [ 'label' => __( 'Palm Tree', 'bw-map-magnet' ),  'svg' => '<path d="M12 22V11"/><path d="M5 8c2-3 5-4 7-3"/><path d="M19 8c-2-3-5-4-7-3"/><path d="M5 8c0-3 3-5 7-5"/><path d="M19 8c0-3-3-5-7-5"/><path d="M12 11c-2-2-4-3-7-3"/><path d="M12 11c2-2 4-3 7-3"/>' ],
			'snorkel'    => [ 'label' => __( 'Snorkel / Mask', 'bw-map-magnet' ), 'svg' => '<path d="M3 10a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v3a4 4 0 0 1-4 4h-2l-3-2-3 2H7a4 4 0 0 1-4-4z"/><path d="M9 13h6"/>' ],
			'scuba'      => [ 'label' => __( 'Scuba Tank', 'bw-map-magnet' ),  'svg' => '<rect x="8" y="7" width="8" height="15" rx="2.5"/><path d="M11 7V5h2v2"/><path d="M13 5h2a2 2 0 0 1 2 2v2"/><path d="M8 12h8"/>' ],
			'boat'       => [ 'label' => __( 'Sailboat', 'bw-map-magnet' ),   'svg' => '<path d="M2 19h20l-3-5H5z"/><path d="M12 14V3"/><path d="M12 3l7 11H5z"/>' ],
			'fishing'    => [ 'label' => __( 'Fishing / Fish', 'bw-map-magnet' ), 'svg' => '<path d="M7 12c1-3 5-5 10-5 2 0 4 .5 5.5 1.5-1 1-1 5 0 6-1.5 1-3.5 1.5-5.5 1.5-5 0-9-2-10-4z"/><path d="M2 8l5 4-5 4z"/><path d="M17 11h.01"/>' ],
			'kayak'      => [ 'label' => __( 'Kayak / Paddle', 'bw-map-magnet' ), 'svg' => '<path d="M12 2v20"/><path d="M9 2h6"/><path d="M8 16c0 3 2 6 4 6s4-3 4-6c0-2-2-3-4-3s-4 1-4 3z"/>' ],
			'wave'       => [ 'label' => __( 'Waves', 'bw-map-magnet' ),      'svg' => '<path d="M2 6c2-2 4-2 6 0s4 2 6 0 4-2 6 0 4 2 6 0"/><path d="M2 12c2-2 4-2 6 0s4 2 6 0 4-2 6 0 4 2 6 0"/><path d="M2 18c2-2 4-2 6 0s4 2 6 0 4-2 6 0 4 2 6 0"/>' ],
			'anchor'     => [ 'label' => __( 'Anchor', 'bw-map-magnet' ),     'svg' => '<circle cx="12" cy="5" r="3"/><path d="M12 8v14"/><path d="M5 12h14"/><path d="M5 12a7 7 0 0 0 14 0"/>' ],
			'restaurant' => [ 'label' => __( 'Restaurant', 'bw-map-magnet' ), 'svg' => '<path d="M5 2v9a2 2 0 0 0 4 0V2"/><path d="M7 2v20"/><path d="M21 15V2a5 5 0 0 0-5 5v6a2 2 0 0 0 2 2h3z"/><path d="M19 15v7"/>' ],
			'cocktail'   => [ 'label' => __( 'Cocktail / Bar', 'bw-map-magnet' ), 'svg' => '<path d="M8 22h8"/><path d="M12 11v11"/><path d="M19 3H5l7 8z"/>' ],
			'coffee'     => [ 'label' => __( 'Coffee / Café', 'bw-map-magnet' ),  'svg' => '<path d="M2 8h14v8a4 4 0 0 1-4 4H6a4 4 0 0 1-4-4z"/><path d="M16 8h2a3 3 0 0 1 0 6h-2"/><path d="M6 2v2M10 2v2M14 2v2"/>' ],
			'spa'        => [ 'label' => __( 'Spa / Wellness', 'bw-map-magnet' ), 'svg' => '<path d="M3 21c0-10 8-18 18-18 0 10-8 18-18 18z"/><path d="M3 21L21 3"/>' ],
			'shopping'   => [ 'label' => __( 'Shopping', 'bw-map-magnet' ),   'svg' => '<path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/>' ],
			'tour'       => [ 'label' => __( 'Tour / Compass', 'bw-map-magnet' ), 'svg' => '<circle cx="12" cy="12" r="10"/><path d="M16 8l-2 6-6 2 2-6z"/>' ],
			'turtle'     => [ 'label' => __( 'Turtle / Wildlife', 'bw-map-magnet' ), 'svg' => '<ellipse cx="12" cy="13" rx="6" ry="5"/><circle cx="12" cy="6" r="2"/><path d="M12 18v3"/><path d="M6 11l-3-1"/><path d="M18 11l3-1"/><path d="M7 16l-3 2"/><path d="M17 16l3 2"/>' ],
			'bird'       => [ 'label' => __( 'Bird', 'bw-map-magnet' ),       'svg' => '<path d="M4 16c0-5 4-9 9-9 5 0 8 3 8 7 0 4-3 7-8 7-5 0-9-2-9-5z"/><path d="M21 14l3 0-3 2z"/><path d="M9 14c2 1 5 1 7 0"/><path d="M17 11h.01"/><path d="M10 21v2"/><path d="M14 21v2"/>' ],
			'lighthouse' => [ 'label' => __( 'Lighthouse', 'bw-map-magnet' ), 'svg' => '<path d="M10 4h4"/><path d="M9 8h6"/><path d="M10 8l-2 14"/><path d="M14 8l2 14"/><path d="M8 22h8"/><path d="M7 4l3 0"/>' ],
			'golf'       => [ 'label' => __( 'Golf', 'bw-map-magnet' ),       'svg' => '<path d="M5 22V3"/><path d="M5 3l13 4-4 4 4 4-13-4"/>' ],
			'tennis'     => [ 'label' => __( 'Tennis', 'bw-map-magnet' ),     'svg' => '<circle cx="12" cy="12" r="9"/><path d="M5 9c4-1 10-1 14 0"/><path d="M5 15c4 1 10 1 14 0"/>' ],
			'music'      => [ 'label' => __( 'Music / Nightlife', 'bw-map-magnet' ), 'svg' => '<circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/><path d="M9 18V5l12-2v13"/>' ],
			'fire'       => [ 'label' => __( 'Fire / Fire Pit', 'bw-map-magnet' ), 'svg' => '<path d="M8.5 14.5A2.5 2.5 0 0 0 11 12c0-1.38-.5-2-1-3-1.072-2.143-.224-4.054 2-6 .5 2.5 2 4.9 4 6.5 2 1.6 3 3.5 3 5.5a7 7 0 1 1-14 0c0-1.153.433-2.294 1-3a2.5 2.5 0 0 0 2.5 2.5z"/>' ],
			'landmark'   => [ 'label' => __( 'Landmark', 'bw-map-magnet' ),   'svg' => '<path d="M3 22h18"/><path d="M5 22V11h14v11"/><path d="M2 11l10-7 10 7"/>' ],
			'sun'        => [ 'label' => __( 'Sun / Beach Day', 'bw-map-magnet' ), 'svg' => '<circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="M4.93 4.93l1.41 1.41"/><path d="M17.66 17.66l1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="M4.93 19.07l1.41-1.41"/><path d="M17.66 6.34l1.41-1.41"/>' ],
		];

		return $cache;
	}

	public static function exists( $key ) {
		$lib = self::library();
		return isset( $lib[ $key ] );
	}

	public static function svg_inner( $key ) {
		$lib = self::library();
		if ( ! isset( $lib[ $key ] ) ) {
			$key = self::DEFAULT_KEY;
		}
		return $lib[ $key ]['svg'];
	}

	/**
	 * Render a full <svg> wrapper around the icon's inner content.
	 */
	public static function render( $key, $size = 18, $class = 'bw-mm-icon' ) {
		$inner = self::svg_inner( $key );
		return sprintf(
			'<svg class="%1$s" viewBox="0 0 24 24" width="%2$d" height="%2$d" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">%3$s</svg>',
			esc_attr( $class ),
			(int) $size,
			$inner // SVG inner content is sourced from the in-code library only — safe to emit. phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		);
	}

	/**
	 * Localizable map: { key => svgInner } for the frontend JS.
	 *
	 * @return array<string,string>
	 */
	public static function js_map() {
		$out = [];
		foreach ( self::library() as $key => $entry ) {
			$out[ $key ] = $entry['svg'];
		}
		return $out;
	}

	public static function get_term_icon( $term_id ) {
		$key = get_term_meta( (int) $term_id, '_bw_map_icon', true );
		if ( ! $key || ! self::exists( $key ) ) {
			return self::DEFAULT_KEY;
		}
		return $key;
	}
}
