<?php


class esigForminatorSetting {


    
    private static $entryValue = null;

    public static function setEntryValue($ID)
    {
        self::$entryValue = $ID;
    }
    public static function getEntryValue()
    {
        return self::$entryValue;
    }

        
    public static function get_sad_documents()
    {
        if (!function_exists('WP_E_Sig'))
        return;

        $api = WP_E_Sig();


        if (!class_exists('esig_sad_document'))
             return;

        $sad = new \esig_sad_document();

        $sad_pages = $sad->esig_get_sad_pages();

        $choices = [];


        foreach ($sad_pages as $page) {
            $document_status = $api->document->getStatus($page->document_id);

            if ($document_status != 'trash') {
                if ('publish' === get_post_status($page->page_id)) {
                    $choices[$page->page_id] = get_the_title($page->page_id);
                    
                }
            }

            
        }

        return $choices;
    }

    public static function getEsignFormSetting($formId,$metaKey){
        return get_post_meta($formId,$metaKey,true);
    }
    
    public static function save_submission_value($document_id,$formData){
        WP_E_Sig()->meta->add($document_id, "esig_forminator_forms_submission_value", json_encode($formData));
    }

    public static function get_submission_value($documentId) 
    {
        return json_decode(WP_E_Sig()->meta->get($documentId, "esig_forminator_forms_submission_value"), true);
    }

    public static function generateValue($forminatorValue,$fieldId,$fieldType,$display,$label,$formid,$displayType) 
        {
          
            $style = '';
            if($displayType == 'underline'){
                $style = 'text-decoration:underline;';
            }
            
            if (is_array($forminatorValue)) 
            {
                // Handle name fields - multi-name fields have nested structure
                if ($fieldType == 'name') {
                    $value = self::get_name_value($forminatorValue, $fieldId);
                }
                // Handle upload fields - may have different structures
                elseif ($fieldType == 'upload') {
                    // Upload fields can have different structures
                    if (isset($forminatorValue[$fieldId])) {
                        $uploadData = $forminatorValue[$fieldId];
                        // Check if it's nested under 'value' key
                        if (is_array($uploadData) && isset($uploadData['value'])) {
                            $value = $uploadData['value'];
                        } else {
                            // Use the data directly
                            $value = $uploadData;
                        }
                    } else {
                        $value = '';
                    }
                }
                // Handle date fields - check for different input types (picker, select, text)
                elseif ($fieldType == 'date') {
                    $value = self::get_date_value($forminatorValue, $fieldId, $formid);
                    if ($value === false) {
                        // Fallback: try to get value from standard location
                        if (isset($forminatorValue[$fieldId]['value'])) {
                            $value = $forminatorValue[$fieldId]['value'];
                        } elseif (isset($forminatorValue[$fieldId]) && is_array($forminatorValue[$fieldId])) {
                            // If the value itself is an array (might be date components), try to format it
                            $date_data = $forminatorValue[$fieldId];
                            if (isset($date_data['day']) && isset($date_data['month']) && isset($date_data['year'])) {
                                // Try to format as date
                                $field = Forminator_API::get_form_field($formid, $fieldId, true);
                                if ($field && class_exists('Forminator_Form_Entry_Model')) {
                                    $meta_value = array(
                                        'day' => $date_data['day'],
                                        'month' => $date_data['month'],
                                        'year' => $date_data['year'],
                                    );
                                    if (isset($field['date_format'])) {
                                        $meta_value['format'] = self::datepicker_default_format($field['date_format']);
                                    }
                                    $value = Forminator_Form_Entry_Model::meta_value_to_string('date', $meta_value, true);
                                } else {
                                    // Last resort: format manually
                                    $value = sprintf('%02d/%02d/%04d', $date_data['month'], $date_data['day'], $date_data['year']);
                                }
                            } else {
                                // If it's an array but not date components, return empty
                                $value = '';
                            }
                        } else {
                            $value = '';
                        }
                    }
                } else {
                    $value = isset($forminatorValue[$fieldId]['value']) ? $forminatorValue[$fieldId]['value'] : '';
                }
               

                if ($display == "label_value") {
                    
                    if ($fieldType == 'email') {
                        return $label . ": ". '<a href="mailto:' . $value . '" target="_blank" style="'.$style.'" >' . $value . '</a>';
                    }
                    if ($fieldType == 'url') {
                        return $label . ": ". '<a href="' . $value . '" target="_blank" style="'.$style.'" >' . $value . '</a>';
                    }                    

                    if ($fieldType == 'address') {

                        $items = '';
                        foreach ($value as $item) {
                            if ($item) {
                                if(end($value) === $item)
                                {
                                    $items .= $item.'';
                                }else{
                                    $items .= $item.', ';
                                }
                                
                            }
                        }
                        return $label . ": " . $items ;
                    }                   
                    
                    if($fieldType == 'time'){
                        $items = '';
                        $count = 1 ;
                        foreach ($value as $item) {
                            if ($item) {

                                if($count == '1'){
                                    $items .= $item.':';
                                }elseif($count == '2'){
                                    $items .= $item.' ';
                                }else{
                                    $items .= $item;
                                }                                
                                $count ++ ;                                
                            }
                        }
                        return $label . ": " . $items ;                                             
                       
                    }

                    if($fieldType == 'calculation'){                                             
                        return $label . ": " . $value['formatting_result'] ;
                    }

                    if($fieldType == 'currency'){                                             
                        $formatted_value = self::format_currency_value($value);
                        return $label . ": " . $formatted_value;
                    }

                    if ($fieldType == 'upload') {                           
                        return $label . ": " . self::get_upload_url($value,$style);
                        
                    }

                    if($fieldType == 'html'){                        
                        $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                        $html = $getField['variations'];
                        return $label . ": " . $html ; 
                       
                    }

                    if($fieldType == 'consent'){                        
                        // Get the consent field to retrieve the consent_description
                        $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                        $consent_description = isset($getField['consent_description']) ? $getField['consent_description'] : '';
                        // Render the consent description as HTML (not escaped)
                        $consent_html = wp_kses_post($consent_description);
                        return $label . ": " . $consent_html ; 
                       
                    }

                    // Handle name fields specifically to ensure proper formatting
                    if($fieldType == 'name'){
                        // Ensure value is a string, not an array
                        if (is_array($value)) {
                            // If value is still an array, try to get name value again
                            $value = self::get_name_value($forminatorValue, $fieldId);
                        }
                        // Ensure value is a string before returning
                        $value = is_string($value) ? $value : '';
                        return $label . ": " . $value;
                    }

                    // Handle date fields specifically to ensure proper formatting
                    if($fieldType == 'date'){
                        // Ensure value is a string, not an array
                        if (is_array($value)) {
                            // Try to format the array as a date
                            if (isset($value['day']) && isset($value['month']) && isset($value['year'])) {
                                $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                                if ($getField && class_exists('Forminator_Form_Entry_Model')) {
                                    $meta_value = array(
                                        'day' => $value['day'],
                                        'month' => $value['month'],
                                        'year' => $value['year'],
                                    );
                                    if (isset($getField['date_format'])) {
                                        $meta_value['format'] = self::datepicker_default_format($getField['date_format']);
                                    }
                                    $value = Forminator_Form_Entry_Model::meta_value_to_string('date', $meta_value, true);
                                } else {
                                    // Last resort: format manually
                                    $value = sprintf('%02d/%02d/%04d', $value['month'], $value['day'], $value['year']);
                                }
                            } else {
                                $value = ''; // Invalid date array structure
                            }
                        }
                        // Ensure value is a string before returning
                        $value = is_string($value) ? $value : '';
                        return $label . ": " . $value;
                    }

                    if($fieldType == 'checkbox' || $fieldType == 'radio' || $fieldType == 'select'){  
                       $optionFields = Forminator_API::get_form_field($formid,$fieldId,true);
                       return self::getOptionsValue($optionFields,$value,$display);
                                               
                    }
                    
                    // Final safeguard: ensure value is never an array
                    if (is_array($value)) {
                        $value = '';
                    }
                    
                    return $label . ": " . $value;


                } elseif($display == 'value'){                    

                    if ($fieldType == 'email') {
                        return '<a href="mailto:' . $value . '" target="_blank" style="'.$style.'" >' . $value . '</a>';
                    }
                    if ($fieldType == 'url') {
                        return '<a href="' . $value . '" target="_blank" style="'.$style.'" >' . $value . '</a>';
                    }

                    // Handle name fields
                    if($fieldType == 'name'){
                        // Ensure value is a string, not an array
                        if (is_array($value)) {
                            // If value is still an array, try to get name value again
                            $value = self::get_name_value($forminatorValue, $fieldId);
                        }
                        // Ensure value is a string before returning
                        $value = is_string($value) ? $value : '';
                        return $value;
                    }

                    if($fieldType == 'calculation'){                       
                        return $value['formatting_result'] ;
                    }

                    if($fieldType == 'currency'){                       
                        return self::format_currency_value($value);
                    }

                    if ($fieldType == 'upload') {                       
                        return self::get_upload_url($value,$style);
                        
                    }
                    if ($fieldType == 'address') {

                        $items = '';
                        foreach ($value as $item) {
                            if ($item) {
                                if(end($value) === $item)
                                {
                                    $items .= $item.'';
                                }else{
                                    $items .= $item.', ';
                                }
                                
                            }
                        }
                        return $items ;
                    }
                    if($fieldType == 'time'){
                        $items = '';
                        $count = 1 ;
                        foreach ($value as $item) {
                            if ($item) {

                                if($count == '1'){
                                    $items .= $item.':';
                                }elseif($count == '2'){
                                    $items .= $item.' ';
                                }else{
                                    $items .= $item;
                                }                                
                                $count ++ ;                                
                            }
                        }
                        return $items ;                                             
                       
                    }

                    // Handle date fields specifically to ensure proper formatting
                    if($fieldType == 'date'){
                        // Ensure value is a string, not an array
                        if (is_array($value)) {
                            // Try to format the array as a date
                            if (isset($value['day']) && isset($value['month']) && isset($value['year'])) {
                                $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                                if ($getField && class_exists('Forminator_Form_Entry_Model')) {
                                    $meta_value = array(
                                        'day' => $value['day'],
                                        'month' => $value['month'],
                                        'year' => $value['year'],
                                    );
                                    if (isset($getField['date_format'])) {
                                        $meta_value['format'] = self::datepicker_default_format($getField['date_format']);
                                    }
                                    $value = Forminator_Form_Entry_Model::meta_value_to_string('date', $meta_value, true);
                                } else {
                                    // Last resort: format manually
                                    $value = sprintf('%02d/%02d/%04d', $value['month'], $value['day'], $value['year']);
                                }
                            } else {
                                // If it's an array but not date components, try to get date value again
                                $value = self::get_date_value($forminatorValue, $fieldId, $formid);
                                if ($value === false) {
                                    $value = '';
                                }
                            }
                        }
                        // Ensure value is a string before returning
                        $value = is_string($value) ? $value : '';
                        return $value;
                    }

                    if($fieldType == 'html'){
                        
                        $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                        $html = $getField['variations'];
                        return $html ; 
                       
                    }
                    if($fieldType == 'consent'){
                        // Get the consent field to retrieve the consent_description
                        $getField = Forminator_API::get_form_field($formid,$fieldId,true);
                        $consent_description = isset($getField['consent_description']) ? $getField['consent_description'] : '';
                        // Render the consent description as HTML (not escaped)
                        return wp_kses_post($consent_description);
                       
                    }
                    if($fieldType == 'checkbox' || $fieldType == 'radio' || $fieldType == 'select'){  
                        $optionFields = Forminator_API::get_form_field($formid,$fieldId,true);
                        return self::getOptionsValue($optionFields,$value,$display);
                                                
                    }
                    
                    // Final safeguard: ensure value is never an array
                    if (is_array($value)) {
                        $value = '';
                    }
                    
                    return $value;
                }else{
                    
                    if($fieldType == 'checkbox' || $fieldType == 'radio' || $fieldType == 'select'){ 
                        return $value;
                    }else{
                        return $label;
                    }
                    
                }
            }
        }

        public static function getOptionsValue($fields,$value,$type){
            $optionValue = '';
                foreach($fields['options'] as $val){
                    if (strpos($value, $val['label']) === false) {
                        continue;
                    }

                    if($type == 'label_value'){                    
                        if($val['value']){
                            $optionValue .= $val['label'] . ": " . $val['value']. ", ";
                        }else{
                            $optionValue .= $val['label'] . ", ";
                        }
                    }else{
                        if($val['value']){
                            $optionValue .= $val['value']. ", ";
                        }else{
                            $optionValue .= $value . ", ";
                        }
                    }                      
                        
                }
            return substr($optionValue, 0, -2); 
        }

        public static function get_upload_url($value,$style){

            // Handle different data structures for upload fields
            $fileUrl = '';
            
            // Check if value is already a string (direct URL)
            if (is_string($value) && !empty($value)) {
                $fileUrl = $value;
            }
            // Check for nested array structure: $value['file']['file_url']
            elseif (is_array($value) && isset($value['file']) && is_array($value['file']) && isset($value['file']['file_url'])) {
                $fileUrl = $value['file']['file_url'];
            }
            // Check for direct array structure: $value['file_url']
            elseif (is_array($value) && isset($value['file_url'])) {
                $fileUrl = $value['file_url'];
            }
            // Check if value itself is an array with URL at root level
            elseif (is_array($value) && isset($value[0]) && is_string($value[0])) {
                $fileUrl = $value;
            }
            
            // If fileUrl is an array (multiple files)
            if (is_array($fileUrl)) {
                $items = '';
                foreach ($fileUrl as $item) {               
                    if ($item && is_string($item)) {
                        $items .=  '<a href="' . esc_url($item) . '" style="' . esc_attr($style) . '" >' . esc_html(basename($item)) . '</a><br>';  
                    }
                }
                return $items;
            }
            // If fileUrl is a string (single file)
            elseif (is_string($fileUrl) && !empty($fileUrl)) {
                return "<a href='" . esc_url($fileUrl) . "' style='" . esc_attr($style) . "' >" . esc_html(basename($fileUrl)) . "</a>";
            }
            
            // Return empty if no valid URL found
            return '';
        }

        /**
         * Format currency value with proper comma punctuation
         * 
         * @param string|array $value The currency value to format
         * @return string Formatted currency value with commas
         */
        public static function format_currency_value($value) {
            // Handle array values (if value is nested in array)
            if (is_array($value)) {
                $value = isset($value['value']) ? $value['value'] : (isset($value[0]) ? $value[0] : '');
            }
            
            // Convert to string if not already
            $value = (string) $value;
            
            // If empty, return as is
            if (empty($value) && $value !== '0') {
                return $value;
            }
            
            // Remove any existing currency symbols and formatting
            // This regex removes common currency symbols and non-numeric characters except decimal point and minus sign
            $numeric_value = preg_replace('/[^\d\.\-]/', '', $value);
            
            // Check if it's a valid number
            if (!is_numeric($numeric_value)) {
                // If not numeric, return original value
                return $value;
            }
            
            // Convert to float for formatting
            $float_value = (float) $numeric_value;
            
            // Format with commas (thousands separator) and preserve decimals
            // Check if original value had decimals
            $has_decimals = (strpos($numeric_value, '.') !== false);
            
            if ($has_decimals) {
                // Format with 2 decimal places
                $formatted = number_format($float_value, 2, '.', ',');
            } else {
                // Format without decimal places
                $formatted = number_format($float_value, 0, '.', ',');
            }
            
            return $formatted;
        }



    public static function enableReminder($reminderSettings,$docId){
        $reminder_set =  esig_forminator_get('enabled_signing_reminder',$reminderSettings);
        $reminderEmail = esig_forminator_get('first_reminder_email',$reminderSettings);
        $first_reminder_send = esig_forminator_get('second_reminder_email',$reminderSettings);
        $expire_reminder = esig_forminator_get('expire_reminder',$reminderSettings);

        if($reminderEmail < 1 || $first_reminder_send < 1 || $expire_reminder < 1)
        {
            return false ; 
        }

        if ($reminder_set == '1') {

            $esig_ff_reminders_settings = array(
                "esig_reminder_for" => esig_forminator_sanitize_init($reminderEmail),
                "esig_reminder_repeat" => esig_forminator_sanitize_init($first_reminder_send),
                "esig_reminder_expire" => esig_forminator_sanitize_init($expire_reminder),
            );

            WP_E_Sig()->meta->add($docId, "esig_reminder_settings_", json_encode($esig_ff_reminders_settings));
            WP_E_Sig()->meta->add($docId, "esig_reminder_send_", "1");
        }
    }


    public static function display_value($forminatorValue, $submit_type) {

        $result = '';
        if ($submit_type == "underline") {
            $result .= '<u>' . $forminatorValue . '</u>';
        } else {
            $result .= $forminatorValue;
        }
        return $result;
    }



    public static function get_value($data,$label,$field_id, $display, $option) {
            

        $label = $label;

        // print_r($data);
        if (is_array($data)) {

            if ($display == "value") {  


                $value = isset($data[$field_id]) ? $data[$field_id] : false;
                $result = '';
                if (is_array($value)) {
                    foreach ($value as $val) {
                        $result .= $val . " ,";
                    } 
                    
                    return  substr($result, 0, strlen($result) - 2);
                }
                
                               
                return  $value;              

            } elseif ($display == "label_value") {                   

                $value = isset($data[$field_id]) ? $data[$field_id] : false;
                $result = '';
                if (is_array($value)) {
                    foreach ($value as $val) {
                        $result .= $val . " ,";
                    } 
                    
                    return $label . ": " . substr($result, 0, strlen($result) - 2);
                }
                
                               
                return $label . ": " . $value;

            } else{
                return $label;
            }
        }
        return false;
    }

    

        public static function parseInput($string)
        {
            $results = preg_replace('/^{(.*)}$/', '$1', $string);
            $array = explode(".", $results);
            return esig_forminator_get("1",$array);
        }

        /**
         * Get formatted date value for datepicker fields
         * Handles all three types: picker (calendar), select (dropdown), and text input
         *
         * @param array $forminatorValue The form submission data
         * @param string $fieldId The field element ID
         * @param int $formid The form ID
         * @return string|false Formatted date string or false on failure
         */
        private static function get_date_value($forminatorValue, $fieldId, $formid) {
            // Get the field settings to determine the input type
            $field = Forminator_API::get_form_field($formid, $fieldId, true);
            if (!$field || !isset($field['field_type'])) {
                return false;
            }

            $date_field_type = $field['field_type']; // 'picker', 'select', or 'text'
            
            // For 'select' type (dropdown), data is stored with day/month/year suffixes
            if ($date_field_type == 'select') {
                $day_element_id = $fieldId . '-day';
                $month_element_id = $fieldId . '-month';
                $year_element_id = $fieldId . '-year';
                $format_element_id = $fieldId . '-format';

                // Extract values, handling nested arrays
                $day = self::extract_date_component($forminatorValue, $day_element_id);
                $month = self::extract_date_component($forminatorValue, $month_element_id);
                $year = self::extract_date_component($forminatorValue, $year_element_id);
                $format = self::extract_date_component($forminatorValue, $format_element_id);

                // If we couldn't find day/month/year with suffixes, check if data is stored directly under fieldId
                if ((!$day || !$month || !$year) && isset($forminatorValue[$fieldId])) {
                    $field_data = $forminatorValue[$fieldId];
                    if (is_array($field_data)) {
                        // Check if it has day/month/year directly
                        if (isset($field_data['day']) && isset($field_data['month']) && isset($field_data['year'])) {
                            $day = $field_data['day'];
                            $month = $field_data['month'];
                            $year = $field_data['year'];
                            if (isset($field_data['format'])) {
                                $format = $field_data['format'];
                            }
                        } elseif (isset($field_data['value']) && is_array($field_data['value'])) {
                            // Check nested value array
                            if (isset($field_data['value']['day']) && isset($field_data['value']['month']) && isset($field_data['value']['year'])) {
                                $day = $field_data['value']['day'];
                                $month = $field_data['value']['month'];
                                $year = $field_data['value']['year'];
                                if (isset($field_data['value']['format'])) {
                                    $format = $field_data['value']['format'];
                                }
                            }
                        }
                    }
                }

                if ($day && $month && $year) {
                    $meta_value = array(
                        'day' => $day,
                        'month' => $month,
                        'year' => $year,
                    );
                    
                    // Get date format from field settings if not in submission data
                    if (empty($format) && isset($field['date_format'])) {
                        $format = self::datepicker_default_format($field['date_format']);
                    } elseif (!empty($format)) {
                        $format = self::datepicker_default_format($format);
                    }
                    
                    if (!empty($format)) {
                        $meta_value['format'] = $format;
                    }

                    // Use Forminator's method to format the date
                    if (class_exists('Forminator_Form_Entry_Model')) {
                        return Forminator_Form_Entry_Model::meta_value_to_string('date', $meta_value, true);
                    }
                }
            } 
            // For 'picker' and 'text' types, the value is stored directly
            elseif (($date_field_type == 'picker' || $date_field_type == 'text')) {
                // Check if value exists in the standard location
                if (isset($forminatorValue[$fieldId])) {
                    $date_data = $forminatorValue[$fieldId];
                    
                    // Check if it's in the standard format with 'value' key
                    if (is_array($date_data) && isset($date_data['value'])) {
                        return $date_data['value'];
                    } 
                    // Or if it's stored directly as a string
                    elseif (is_string($date_data)) {
                        return $date_data;
                    }
                    // Or if it's an array with day/month/year (some edge cases)
                    elseif (is_array($date_data) && isset($date_data['day']) && isset($date_data['month']) && isset($date_data['year'])) {
                        $meta_value = array(
                            'day' => $date_data['day'],
                            'month' => $date_data['month'],
                            'year' => $date_data['year'],
                        );
                        
                        if (isset($field['date_format'])) {
                            $meta_value['format'] = self::datepicker_default_format($field['date_format']);
                        }
                        
                        if (class_exists('Forminator_Form_Entry_Model')) {
                            return Forminator_Form_Entry_Model::meta_value_to_string('date', $meta_value, true);
                        }
                    }
                }
            }

            return false;
        }

        /**
         * Extract date component value from form data, handling nested arrays
         *
         * @param array $data The form submission data
         * @param string $key The key to extract
         * @return mixed The extracted value or null
         */
        private static function extract_date_component($data, $key) {
            if (!isset($data[$key])) {
                return null;
            }
            $val = $data[$key];
            // Handle nested array with 'value' key
            if (is_array($val) && isset($val['value'])) {
                $val = $val['value'];
            }
            // Ensure we return a scalar value, not an array
            if (is_array($val)) {
                // If it's an array, try to get the first element or return null
                return !empty($val) ? reset($val) : null;
            }
            return $val;
        }

        /**
         * Get formatted name value for name fields
         * Handles both single name fields and multi-name fields (with sub-fields like first-name, last-name)
         *
         * @param array $forminatorValue The form submission data
         * @param string $fieldId The field element ID
         * @return string Formatted name string
         */
        private static function get_name_value($forminatorValue, $fieldId) {
            if (!isset($forminatorValue[$fieldId])) {
                return '';
            }
            
            $fieldData = $forminatorValue[$fieldId];
            
            // Check if it's a multi-name field with nested structure
            if (is_array($fieldData)) {
                // Check for nested value structure: $forminatorValue['name-2']['value']['first-name']
                if (isset($fieldData['value']) && is_array($fieldData['value'])) {
                    $nameParts = array();
                    
                    // Order of sub-fields to combine
                    $subFields = array('prefix', 'first-name', 'middle-name', 'last-name');
                    
                    foreach ($subFields as $subField) {
                        if (isset($fieldData['value'][$subField]) && !empty($fieldData['value'][$subField])) {
                            $nameParts[] = trim($fieldData['value'][$subField]);
                        }
                    }
                    
                    if (!empty($nameParts)) {
                        return implode(' ', $nameParts);
                    }
                }
                // Check for direct structure: $forminatorValue['name-2']['first-name']
                elseif (isset($fieldData['first-name']) || isset($fieldData['last-name'])) {
                    $nameParts = array();
                    
                    $subFields = array('prefix', 'first-name', 'middle-name', 'last-name');
                    
                    foreach ($subFields as $subField) {
                        if (isset($fieldData[$subField]) && !empty($fieldData[$subField])) {
                            $nameParts[] = trim($fieldData[$subField]);
                        }
                    }
                    
                    if (!empty($nameParts)) {
                        return implode(' ', $nameParts);
                    }
                }
                // If it's an array but not a multi-name structure, try to get 'value' key
                elseif (isset($fieldData['value'])) {
                    return is_string($fieldData['value']) ? $fieldData['value'] : '';
                }
            }
            
            // Fallback: if it's a string, return it directly
            if (is_string($fieldData)) {
                return $fieldData;
            }
            
            return '';
        }

        /**
         * Convert Forminator date format to PHP date format
         * 
         * @param string $format Forminator date format (e.g., 'mm/dd/yy')
         * @return string PHP date format (e.g., 'm/d/Y')
         */
        private static function datepicker_default_format($format) {
            if (function_exists('datepicker_default_format')) {
                return datepicker_default_format($format);
            }
            
            // Fallback conversion if function doesn't exist
            switch ($format) {
                case 'mm/dd/yy':
                    return 'm/d/Y';
                case 'mm.dd.yy':
                    return 'm.d.Y';
                case 'mm-dd-yy':
                    return 'm-d-Y';
                case 'yy-mm-dd':
                    return 'Y-m-d';
                case 'yy.mm.dd':
                    return 'Y.m.d';
                case 'yy/mm/dd':
                    return 'Y/m/d';
                case 'dd/mm/yy':
                    return 'd/m/Y';
                case 'dd.mm.yy':
                    return 'd.m.Y';
                case 'dd-mm-yy':
                    return 'd-m-Y';
                default:
                    return get_option('date_format', 'm/d/Y');
            }
        }



    
}
