<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Scan & update Customizer / theme_mod texts (Kirki, etc.).
 *
 * @since 2.2.0
 */
class AI_Magic_Customizer_Scanner {

    /**
     * Collect theme_mods that look like textual content.
     *
     * @return array
     */
    public function get_items() {
        $items = array();
        $mods  = get_theme_mods();

        if ( ! is_array( $mods ) ) {
            return $items;
        }

        foreach ( $mods as $key => $value ) {
            if ( ! is_string( $value ) ) {
                continue;
            }

            $raw = trim( wp_strip_all_tags( $value ) );
            if ( $raw === '' || strlen( $raw ) < 3 ) {
                continue;
            }

            $items[] = array(
                'id'    => $key,
                'value' => $value,
            );
        }

        return $items;
    }

    /**
     * Apply AI-generated values back to theme_mods.
     *
     * @param array $items
     * @param array $new_values
     * @return void
     */
    public function apply_new_values( array $items, array $new_values ) {
        foreach ( $items as $index => $item ) {
            if ( ! isset( $new_values[ $index ] ) ) {
                continue;
            }
            $new_val = $new_values[ $index ];
            set_theme_mod( $item['id'], $new_val );
        }
    }

}
