<?php

namespace GriffinForms\Admin\Js\Local;

trait inputValidations
{
    public function allValidations()
    {
        $js = '';
        $js .= $this->isValidUrl();
        $js .= $this->isValidEmail();
        $js .= $this->isValidNumber();
        $js .= $this->isValidDate();
        $js .= $this->isValidPattern();
        $js .= $this->isValidNumberPattern();
        $js .= $this->isValidPositiveInteger();
        $js .= $this->isValidCountryCode();
        $js .= $this->hasDuplicates();
        
        return $js; // Return all validation JS as one concatenated string
    }

    public function isValidUrl()
    {
        return 'function isValidUrl(url) {
                var url_validate = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
                if(!url_validate.test(url)){
                    return false;
                } else {
                    return true;
                }
        };' . PHP_EOL;
    }

    public function isValidEmail()
    {
        return 'function isValidEmail(email) {
                var email_validate = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                return email_validate.test(email);
                }' . PHP_EOL;
    }

    public function isValidNumber()
    {
        return 'function isValidNumber(str, decimal = false) {
                    str = str.trim();
                    // Check if input is empty
                    if (str === "") {
                        return false;
                    }
    
                    // Check for scientific notation and reject it
                    if (/e/i.test(str)) {
                        return false;
                    }
    
                    // Handle decimals if the flag is true
                    if (decimal) {
                        // Check if its a valid floating-point number
                        return /^-?\d*(\.\d+)?$/.test(str);
                    } else {
                        // Check if its a valid integer
                        return /^-?\d+$/.test(str);
                    }
                }' . PHP_EOL;
    }

    public function isValidDate()
    {
        return 'function isValidDate(dateVal) {
                if (isNaN(dateVal.getTime())) {
                    return false;
                } else {
                    return true;
                }
            }' . PHP_EOL;
    }

    public function isValidPattern()
    {
        return 'function isValidPattern(pattern) {
            try {
                new RegExp(pattern);
                return true;
            } catch (e) {
                return false;
            }
        }' . PHP_EOL;
    }

    public function isValidNumberPattern()
    {
        return 'function isValidNumberPattern(pattern) {
            try {
                const regex = new RegExp(pattern);
                const numericSamples = ["123", "-45.6", "0"];
                const matchesNumber = numericSamples.some(sample => regex.test(sample));

                const alphabetSamples = ["a", "abc", "X", "xyz", "hello"];
                const blocksLetters = alphabetSamples.every(sample => !regex.test(sample));

                return matchesNumber && blocksLetters;
            } catch (e) {
                return false;
            }
        }' . PHP_EOL;
    }

    public function isValidPositiveInteger()
    {
        return 'function isValidPositiveInteger(str) {
                str = str.trim();
                return /^\d+$/.test(str);
            }' . PHP_EOL;
    }

    public function isValidCountryCode()
    {
        return 'function isValidCountryCode(code) {
                return /^[0-9]{1,4}$/.test(code);
            }' . PHP_EOL;
    }

    public function hasDuplicates()
    {
        return 'function hasDuplicates(list) {
                const seen = new Set();
                for (const item of list) {
                    if (seen.has(item)) {
                        return item;
                    }
                }
                return null;
            }' . PHP_EOL;
    }
}
