<?php

use lib\PAC_DRH\MobileDetect;

/**
 * Creates a DOMDocument object from HTML content.
 *
 * @param string $html
 *
 * @return DOMDocument|null
 */
if (!function_exists('pac_drh_create_dom')):
    function pac_drh_create_dom($html)
    {
        $dom = new DOMDocument('1.0', 'UTF-8');
        $html = mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, 0xFFFF], 'UTF-8');
        $dom->loadHTML(
            '<?xml version="1.0" encoding="UTF-8"?>'."\n".$html,
            LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
        );
        $dom->encoding = 'UTF-8';

        return $dom;
    }
endif;
/**
 * Retrieves the name of the theme options for the PAC DRH theme.
 * This function determines the name of the theme options based on the currently active theme.
 * If the active theme is a child theme, it retrieves the name of the parent theme.
 * If the active theme is 'Divi', it returns 'et_divi' as the theme options name, otherwise, it returns 'et_extra'.
 * @return string
 */
if (!function_exists('pac_drh_get_theme_option_name')):
    function pac_drh_get_theme_option_name()
    {
        $theme_info = wp_get_theme();
        $active_theme = is_child_theme() ? $theme_info->parent()->get('Name') : $theme_info->get('Name');

        return 'divi' === strtolower($active_theme) ? 'et_divi' : 'et_extra';
    }
endif;
/**
 * Retrieves the theme option name based on the active theme.
 * @return string
 */
if (!function_exists('pac_drh_get_option')):
    function pac_drh_get_option($option_name, $default = null)
    {
        if (function_exists('et_get_option')) {
            return et_get_option($option_name);
        }
        if (function_exists('get_option')) {
            $options = get_option(pac_drh_get_theme_option_name());
            if (isset($options[$option_name])) {
                return $options[$option_name];
            }
        }

        return $default;
    }
endif;
/**
 * Check if a specific setting is enabled.
 *
 * @param string $option_name
 *
 * @return bool
 */
if (!function_exists('pac_drh_get_settings_enabled')):
    function pac_drh_get_settings_enabled($option_name)
    {
        return 'on' === pac_drh_get_option($option_name, 'off');
    }
endif;
/**
 * Retrieves custom responsive presets for different device sizes.
 * @return array
 */
if (!function_exists('pac_drh_custom_presets')):
    function pac_drh_custom_presets()
    {
        return [
            'pac_drh_phone_preset_one' => pac_drh_get_option('pac_drh_phone_preset_one', '320'),
            'pac_drh_phone_preset_two' => pac_drh_get_option('pac_drh_phone_preset_two', '479'),
            'pac_drh_phone_preset_three' => pac_drh_get_option('pac_drh_phone_preset_three', '767'),
            'pac_drh_tablet_preset_one' => pac_drh_get_option('pac_drh_tablet_preset_one', '768'),
            'pac_drh_tablet_preset_two' => pac_drh_get_option('pac_drh_tablet_preset_two', '880'),
            'pac_drh_tablet_preset_three' => pac_drh_get_option('pac_drh_tablet_preset_three', '980'),
            'pac_drh_desktop_preset_one' => pac_drh_get_option('pac_drh_desktop_preset_one', '982'),
            'pac_drh_desktop_preset_two' => pac_drh_get_option('pac_drh_desktop_preset_two', '1024'),
            'pac_drh_desktop_preset_three' => pac_drh_get_option('pac_drh_desktop_preset_three', '1200'),
        ];
    }
endif;
/**
 * Check if the function 'pac_drh_detect_mobile' exists, if not, define it.
 * @return MobileDetect
 */
if (!function_exists('pac_drh_detect_mobile')):
    function pac_drh_detect_mobile()
    {
        static $instance = null;
        if (null === $instance) {
            $instance = new MobileDetect();
        }

        return $instance;
    }
endif;
/**
 * Defines custom media queries for the PAC DRH theme.
 * @return array
 */
if (!function_exists('pac_drh_custom_media_queries')):
    function pac_drh_custom_media_queries()
    {
        return [
            'one',
            'two',
            'three',
            'four',
            'five',
        ];
    }
endif;
/**
 * Retrieve the color scheme for the PAC DRH theme.
 * @return string
 */
if (!function_exists('pac_drh_get_color_scheme')):
    function pac_drh_get_color_scheme()
    {
        $color_schemes = et_get_option('color_schemes', 'none');
        switch ($color_schemes) {
            case 'green':
                $color = "#7cc68d";
                break;
            case 'orange':
                $color = "#edb059";
                break;
            case 'pink':
                $color = "#c37cc6";
                break;
            case 'red':
                $color = "#cd5c5c";
                break;
            default:
                $color = et_get_option('accent_color', '#2ea3f2');
        }

        return $color;
    }
endif;
/**
 * Retrieves an array of IDs of all published pages on the site.
 * @return array
 */
if (!function_exists('pac_drh_get_pages_list')):
    function pac_drh_get_pages_list()
    {
        $pages_ids = [];
        $pages = get_pages('hide_empty=0');
        if (!empty($pages)) {
            foreach ($pages as $page) {
                $pages_ids[] = $page->ID;
            }
        }

        return array_map('intval', $pages_ids);
    }
endif;
/**
 * Recursively searches and replaces values within a multidimensional array.
 *
 * @param array $array
 * @param string $search_word
 * @param string $replace_word
 *
 * @return array
 */
if (!function_exists('pac_drh_search_replace_array')):
    function pac_drh_search_replace_array($array, $search_word, $replace_word)
    {
        foreach ($array as $key => &$value) {
            if (is_array($value)) {
                $value = pac_drh_search_replace_array($value, $search_word, $replace_word);
            } else if (is_string($value)) {
                $value = str_replace($search_word, $replace_word, $value);
            }
        }

        return $array;
    }
endif;
/**
 * Retrieve shadow presets values.
 *
 * @param string|null $preset
 *
 * @return array|array[]
 */
if (!function_exists('pac_drh_get_shadow_presets_values')):
    function pac_drh_get_shadow_presets_values($preset = null)
    {
        $presets = [
            'none' => [
                'horizontal' => '',
                'vertical' => '',
                'blur' => '',
                'spread' => '',
                'position' => '',
            ],
            'preset1' => [
                'horizontal' => '0px',
                'vertical' => '2px',
                'blur' => '18px',
                'spread' => '0px',
                'position' => '',
            ],
            'preset2' => [
                'horizontal' => '6px',
                'vertical' => '6px',
                'blur' => '18px',
                'spread' => '0px',
                'position' => '',
            ],
            'preset3' => [
                'horizontal' => '0px',
                'vertical' => '12px',
                'blur' => '18px',
                'spread' => '-6px',
                'position' => '',
            ],
            'preset4' => [
                'horizontal' => '10px',
                'vertical' => '10px',
                'blur' => '0px',
                'spread' => '0px',
                'position' => '',
            ],
            'preset5' => [
                'horizontal' => '0px',
                'vertical' => '6px',
                'blur' => '0px',
                'spread' => '10px',
                'position' => '',
            ],
            'preset6' => [
                'horizontal' => '0px',
                'vertical' => '0px',
                'blur' => '18px',
                'spread' => '0px',
                'position' => 'inset',
            ],
            'preset7' => [
                'horizontal' => '10px',
                'vertical' => '10px',
                'blur' => '0px',
                'spread' => '0px',
                'position' => 'inset',
            ],
        ];
        if (!empty($preset) && isset($presets[$preset])) {
            return $presets[$preset];
        }

        return $presets;
    }
endif;
// phpcs:disable
/**
 * Log messages to a specified file.
 *
 * @param mixed $message
 * @param bool $delete
 * @param string $filename
 *
 * @return void
 */
if (!function_exists('pac_drh_log')):
    function pac_drh_log($message, $delete = false, $filename = '')
    {
        $filename = empty($filename) ? debug_backtrace()[1]['function'] : $filename;
        $log_filepath = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.$filename.'.log';
        if ($delete && file_exists($log_filepath)) {
            wp_delete_file($log_filepath);
        }
        ini_set('error_log', $log_filepath);
        $log = is_array($message) || is_object($message) ? print_r($message, true) : $message;
        error_log($log);
    }
endif;
/**
 * Output debug data and optionally halt execution.
 *
 * @param mixed $data
 * @param bool $exit
 */
if (!function_exists('pac_drh_dd')):
    function pac_drh_dd($data = '', $exit = true)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        if ($exit) {
            exit;
        }
    }
endif;
// phpcs:enable