<?php

/**
 * Term meta registry.
 *
 * @package B3WVS\VariationSwatches\Admin\TermMeta
 */
namespace B3WVS\VariationSwatches\Admin\TermMeta;

defined( 'ABSPATH' ) || exit;
/**
 * Term Meta Registry - Handles registration of term meta fields
 */
class Registry {
    /**
     * Register all term meta fields for product attributes
     */
    public static function register() {
        $attribute_taxonomies = wc_get_attribute_taxonomies();
        foreach ( $attribute_taxonomies as $tax ) {
            $taxonomy = wc_attribute_taxonomy_name( $tax->attribute_name );
            register_term_meta( $taxonomy, 'b3_wvs_type', array(
                'show_in_rest'      => true,
                'single'            => true,
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'auth_callback'     => function () {
                    return current_user_can( 'manage_woocommerce' );
                },
            ) );
            register_term_meta( $taxonomy, 'b3_wvs_color', array(
                'show_in_rest'      => true,
                'single'            => true,
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_hex_color',
                'auth_callback'     => function () {
                    return current_user_can( 'manage_woocommerce' );
                },
            ) );
            register_term_meta( $taxonomy, 'b3_wvs_label', array(
                'show_in_rest'      => true,
                'single'            => true,
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'auth_callback'     => function () {
                    return current_user_can( 'manage_woocommerce' );
                },
            ) );
        }
    }

    /**
     * Register form hooks for all attribute taxonomies
     */
    public static function register_form_hooks() {
        $attribute_taxonomies = wc_get_attribute_taxonomies();
        foreach ( $attribute_taxonomies as $tax ) {
            $taxonomy = wc_attribute_taxonomy_name( $tax->attribute_name );
            add_action( $taxonomy . '_add_form_fields', array(Form::class, 'render_add_fields') );
            add_action(
                $taxonomy . '_edit_form_fields',
                array(Form::class, 'render_edit_fields'),
                10,
                2
            );
            add_action( 'created_' . $taxonomy, array(Sanitizer::class, 'save') );
            add_action( 'edited_' . $taxonomy, array(Sanitizer::class, 'save') );
        }
    }

}
