<?php
/**
 * Importer Factory class.
 *
 * @package SaveToWishlist\Classes\Importers
 * @since 1.0.0
 */
namespace SaveToWishlist\Classes\Importers;

/**
 * Class Importer_Factory
 *
 * Factory to instantiate the correct importer.
 *
 * @since 1.0.0
 */
class Importer_Factory {

    /**
     * Transient key for cached `get_detected_importers()` result.
     *
     * The notice on every SaveTo admin page renders this list, so it is
     * cached briefly to avoid re-running each importer's `is_source_active()`
     * check on every request.
     */
    const DETECTED_CACHE_KEY = 'stwlite_importer_detected';

    /**
     * Cache TTL for the detected-importers list.
     */
    const DETECTED_CACHE_TTL = 5 * MINUTE_IN_SECONDS;

    /**
     * Available importers.
     *
     * @var array
     */
    private static $importers = array(
        'yith'        => 'YITH_Importer',
        'ti'          => 'TI_Importer',
        'moreconvert' => 'MoreConvert_Importer',
        'wpc'         => 'WPC_Importer',
    );

    /**
     * Get available importers.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public static function get_available_importers(): array {
        /**
         * APPLY_FILTERS: stwlite_available_importers
         *
         * Filter the available importers.
         *
         * @param array $importers Available importers.
         *
         * @return array
         */
        return apply_filters( 'stwlite_available_importers', self::$importers );
    }

    /**
     * Get importer instance.
     *
     * @param string $slug Importer slug.
     * @since 1.0.0
     * @access public
     * @return Abstract_Importer|null
     */
    public static function get_importer( string $slug ): ?Abstract_Importer {
        $importers = self::get_available_importers();

        if ( ! isset( $importers[ $slug ] ) ) {
            return null;
        }

        $class_name      = $importers[ $slug ];
        $full_class_name = 'SaveToWishlist\\Classes\\Importers\\' . $class_name;

        if ( ! class_exists( $full_class_name ) ) {
            return null;
        }

        return $full_class_name::instance();
    }

    /**
     * Get all detected importers.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public static function get_detected_importers(): array {
        $cached = get_transient( self::DETECTED_CACHE_KEY );
        if ( is_array( $cached ) ) {
            return $cached;
        }

        $importers = self::get_available_importers();
        $detected  = array();

        foreach ( $importers as $slug => $class_name ) {
            $importer = self::get_importer( $slug );
            if ( $importer && $importer->is_source_active() ) {
                $detected[] = array(
                    'slug'       => $slug,
                    'name'       => $importer->get_source_name(),
                    'icon'       => $importer->get_source_icon(),
                    'version'    => $importer->get_source_version(),
                    'can_import' => true,
                );
            }
        }

        set_transient( self::DETECTED_CACHE_KEY, $detected, self::DETECTED_CACHE_TTL );

        return $detected;
    }

    /**
     * Invalidate the cached detected-importers list.
     *
     * Call after operations that may change which sources are active or have
     * data — e.g. when an import job completes.
     *
     * @since 1.0.6
     * @access public
     * @return void
     */
    public static function flush_detected_cache(): void {
        delete_transient( self::DETECTED_CACHE_KEY );
    }
}
