<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

use PWH_DCFH\App\Controllers\PWH_DCFH_DOM;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Modify_Misc_Field')) {
    class PWH_DCFH_Modify_Misc_Field
    {
        public $render_slug;

        public $output;

        public $props = [];

        /**
         * Class Constructor
         */
        public function __construct($render_slug, $output, $props)
        {
            $this->render_slug = $render_slug;
            $this->output = $output;
            $this->props = $props;
        }

        /**
         * Render Output
         * @return false|string
         */
        public function output()
        {
            if (isset($this->props['use_custom_phone_format']) && 'on' === $this->props['use_custom_phone_format']) {
                $phone_format = !empty($this->props['custom_phone_format']) ? $this->props['custom_phone_format'] : '';
                //$allowed_symbols = !empty($this->props['allowed_symbols']) ? $this->props['allowed_symbols'] : 'all';
                $dom = PWH_DCFH_DOM::dom($this->output);
                $input = $dom->getElementsByTagName('input');
                if (isset($input->length) && 0 !== $input->length) {
                    $input_item = $input->item(0);
                    $input_item->setAttribute('class', $input_item->getAttribute('class').' et_pb_phone_format');
                    $input_item->setAttribute('data-mask-format', $phone_format);
                    //$input_item->setAttribute('data-allowed-symbols', $allowed_symbols);
                    $this->output = $dom->saveHTML();
                }
            }

            return $this->output;
        }

    }
}