<?php
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
function country(){
    return new \twa\htmlhelpers\helpers\countryHelper;
}

function image(){
    return new \twa\htmlhelpers\helpers\imageHelper;
}

function seo(){
    return new \twa\htmlhelpers\helpers\seoHelper;
}

function gtag(){
    return new \twa\htmlhelpers\helpers\gaTrackingHelper;
}
function fbq(){
    return new \twa\htmlhelpers\helpers\fbqTrackingHelper;
}
function structuredData(){
    return new \twa\htmlhelpers\helpers\structuredData;
}

function twaURL(){
    return new \twa\htmlhelpers\helpers\urlHelper();
}

function twaDate(){
    return new \twa\htmlhelpers\helpers\dateHelper();
}

function fallback($var1 = NULL,$var2 = NULL,$var3 = NULL,$var4 = NULL,$var5 = NULL){
    $arr = collect([$var1,$var2,$var3,$var4,$var5])->filter(function($value){
        if($value && $value != ''){ return $value; }
    })->first();

    if($arr){ return $arr; }else{ return null; }

}



function twaArabicNumber($str){
        $arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
        $arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        return str_replace($arabic_western, $arabic_eastern, $str);
}

function twaJsonDecode($value){
    if($value && $value != ''){
        try{
            $value =  json_decode($value,1);
        }catch (Throwable $e){
            $value = [];
        }
    }else{
        $value = [];
    }

    return $value;
}


function picture($src,$alt,$extra_classes = NULL, $extra_html_tags = NULL){
    $extension = pathinfo($src, PATHINFO_EXTENSION);
    if(Str::contains($extension,'?')){
        $extension = substr($extension, 0, strpos($extension, "?"));;
    }
    $src_original = $src;
    $src_webp = str_replace('.'.$extension,'.webp',$src);

    return '<picture loading="lazy" class="'.$extra_classes.'" '.$extra_html_tags.' alt="'.$alt.'">
                <source srcset="'.$src_webp.'" type="image/webp">
                <img loading="lazy" src="'.$src_original.'" alt="'.$alt.'">
            </picture>';
}

?>
