<?php
if (WC_ENABLED) {
    function deep_merge_array($arr1, $arr2)
    {
        // ak to niesu polia tak nemam co mergovat
        // a vratim teda druhu hodnotu
        if (! is_array($arr2) || ! is_array($arr1)) {
            return $arr2;
        }
        // prejdem si pole a pomergujem
        foreach ($arr2 as $key => $value) {
            if (array_key_exists($key, $arr1)) {
                $arr1[$key] = deep_merge_array($arr1[$key], $value);
            } else {
                $arr1[$key] = $value;
            }
        }
        return $arr1;
    }

    function inoby_setup_checkout_fields($checkout_fields)
    {
        $cfg = Inoby_Config::checkout_fields();
        return deep_merge_array($checkout_fields, $cfg);
    }

    function inoby_setup_company_checkout_fields($args)
    {
        $cfg        = Inoby_Config::company_checkout_fields();
        $fields_cfg = $cfg[$args["id"]] ?? false;
        if ($fields_cfg) {
            $args = deep_merge_array($args, $fields_cfg);
        }
        return $args;
    }

    function inoby_add_inplace_input_label_classes($args)
    {
        if ($args["placeholder"] ?? false) {
            $args["label_class"] = [ ...is_array($args["label_class"]) ? $args["label_class"] : explode(" ", $args["label_class"]), "inplace-label"];
        }
        return $args;
    }

    // remove default woocommerce theme;
    add_filter("woocommerce_enqueue_styles", function ($enqueue_styles) {
        // unset($enqueue_styles["woocommerce-general"]); // Remove the gloss
        // unset($enqueue_styles["woocommerce-layout"]); // Remove the layout
        // unset($enqueue_styles["woocommerce-smallscreen"]); // Remove small screen support
        return $enqueue_styles;
    });

    add_filter("woocommerce_checkout_fields", "inoby_setup_checkout_fields");
    add_filter("woocommerce_form_field_args", "inoby_setup_company_checkout_fields", 10);
    add_filter("woocommerce_form_field_args", "inoby_add_inplace_input_label_classes", 11);

    //Remove woocommerce breadcrumbs
    remove_action("woocommerce_before_main_content", "woocommerce_breadcrumb", 20, 0);

    // Remove woocommerce sale flash on single product
    remove_action("woocommerce_before_single_product_summary", "woocommerce_show_product_sale_flash", 10);

    //pridanie placeholderov ku shipping poliam v pokladni
    add_filter("woocommerce_checkout_fields", "override_billing_checkout_fields", 20, 1);
    function override_billing_checkout_fields($fields)
    {
        $fields["shipping"]["shipping_first_name"]["placeholder"] = "Meno";
        $fields["shipping"]["shipping_last_name"]["placeholder"]  = "Priezvisko";
        $fields["shipping"]["shipping_company"]["placeholder"]    = "Názov firmy (voliteľné)";
        $fields["shipping"]["shipping_postcode"]["placeholder"]   = "PSČ";
        $fields["shipping"]["shipping_city"]["placeholder"]       = "Mesto";
        $fields["shipping"]["shipping_country"]["placeholder"]    = "Krajina";

        return $fields;
    }

    if (Inoby_Config::disable_default_shipping_method()) {
        add_filter("woocommerce_shipping_chosen_method", "__return_false", 99);
    }

    /**
     * Remove "Description" Heading Title @ WooCommerce Single Product Tabs
     */
    add_filter("woocommerce_product_description_heading", "__return_null");
    add_filter("woocommerce_product_additional_information_heading", "__return_null");

    /*
   * Zmena textov stock_status podla configu
   * vytiahnut ho vies cez funkciu wc_get_stock_html
   */
    add_filter(
        "woocommerce_get_availability",
        function ($av, $product) {
            $stock_status       = $product->get_stock_status();
            $status_text        = Inoby_Config::stock_status()[$stock_status] ?? "";
            $av["availability"] = $status_text;
            $av["class"]        = $stock_status;
            return $av;
        },
        10,
        2,
    );

    /**
     * Zapinam podporu woocommerce nastaveni pre temu
     */
    add_action("after_setup_theme", function () {
        add_theme_support("woocommerce");
    });

    add_action("rc_filter_engine", function () {
        return Inoby_Config::rc_filter_engine();
    });

    // WC search spomaluje query touto fn
    add_action("init", function () {
        remove_filter("image_downsize", ["WooCommerce_Product_Search_Thumbnail", "image_downsize"], 10);
    });

    if (Inoby_Config::update_sale_special()) {
        /**
         * Výpredaj - zobrazovanie cez product_specials
         */
        function show_sale_products_in_vypredaj_specials()
        {
            $sale_product_ids = wc_get_product_ids_on_sale();

            if (! empty($sale_product_ids)) {
                $vypredaj_term = get_term_by('name', 'Výpredaj', 'product_specials');

                if (! $vypredaj_term) {
                    $new_term = wp_insert_term('Výpredaj', 'product_specials');
                    if (is_wp_error($new_term)) {
                        return;
                    }
                    $vypredaj_term_id = $new_term['term_id'];
                } else {
                    $vypredaj_term_id = $vypredaj_term->term_id;
                }

                foreach ($sale_product_ids as $product_id) {
                    if (! has_term($vypredaj_term_id, 'product_specials', $product_id)) {
                        wp_set_object_terms($product_id, $vypredaj_term_id, 'product_specials', true);
                    }
                }
            }
        }

        function register_sale_specials_cron()
        {
            if (! wp_next_scheduled('update_sale_specials_cron')) {
                wp_schedule_event(time(), 'daily', 'update_sale_specials_cron');
            }
        }
        add_action('wp', 'register_sale_specials_cron');

        function update_sale_specials_cron_job()
        {
            show_sale_products_in_vypredaj_specials();
        }
        add_action('update_sale_specials_cron', 'update_sale_specials_cron_job');

        function remove_sale_specials_cron()
        {
            $timestamp = wp_next_scheduled('update_sale_specials_cron');
            wp_unschedule_event($timestamp, 'update_sale_specials_cron');
        }
        register_deactivation_hook(__FILE__, 'remove_sale_specials_cron');
    }

    function modify_product_specials_taxonomy()
    {
        global $wp_taxonomies;

        if (isset($wp_taxonomies['product_specials'])) {
            $wp_taxonomies['product_specials']->public       = true;
            $wp_taxonomies['product_specials']->show_in_rest = true;
        }
    }
    add_action('init', 'modify_product_specials_taxonomy', 11);
}
