<?php
/**
 * Gutenberg dashicon slugs for blocks.
 *
 * @package JOCBM
 *
 * Copyright (C) 2026 JayOnCode
 * License: GPL-2.0-or-later
 */

namespace JOCBM\Services;

defined( 'ABSPATH' ) || exit;

/**
 * Class BlockIcons
 */
final class BlockIcons {

	/**
	 * Dashicon slug from category or system slug.
	 *
	 * @param string $slug Category or system slug.
	 */
	public static function from_slug( string $slug ): string {
		$key = strtolower( $slug );
		if ( str_contains( $key, 'hero' ) ) {
			return 'cover-image';
		}
		if ( str_contains( $key, 'button' ) ) {
			return 'button';
		}
		if ( str_contains( $key, 'banner' ) ) {
			return 'megaphone';
		}
		if ( str_contains( $key, 'blog' ) || str_contains( $key, 'card' ) ) {
			return 'grid-view';
		}
		if ( str_contains( $key, 'testimonial' ) || str_contains( $key, 'quote' ) ) {
			return 'format-quote';
		}
		if ( str_contains( $key, 'faq' ) || str_contains( $key, 'accordion' ) ) {
			return 'editor-help';
		}
		if ( str_contains( $key, 'pricing' ) || str_contains( $key, 'price' ) ) {
			return 'money-alt';
		}
		if ( str_contains( $key, 'cta' ) || str_contains( $key, 'call' ) ) {
			return 'megaphone';
		}
		if ( str_contains( $key, 'logo' ) || str_contains( $key, 'brand' ) ) {
			return 'images-alt2';
		}
		if ( str_contains( $key, 'feature' ) || str_contains( $key, 'service' ) ) {
			return 'star-filled';
		}
		if ( str_contains( $key, 'saas' ) ) {
			return 'admin-site-alt3';
		}
		return 'category';
	}

	/**
	 * Resolve icon for a library catalog row.
	 *
	 * @param array<string, mixed> $row DB or formatted row.
	 */
	public static function for_library_row( array $row ): string {
		$custom = sanitize_key( (string) ( $row['icon'] ?? '' ) );
		if ( '' !== $custom && 'portfolio' !== $custom ) {
			return $custom;
		}

		$slug = (string) ( $row['category_slug'] ?? $row['categorySlug'] ?? $row['category'] ?? '' );
		if ( '' === $slug ) {
			$slug = (string) ( $row['slug'] ?? $row['systemSlug'] ?? '' );
		}

		return self::from_slug( $slug );
	}
}
