<?php

use WHMCS\Database\Capsule;

if (!function_exists("suiten_koreksinomor")) {

    function suiten_koreksinomor($negara, $inputnomor)
	{
		$global_module_name = 'Hostinoz Whatsapp API';

		// Ambil Data Config
		foreach (WHMCS\Database\Capsule::table('s10wa_config')->WHERE('suiten_Nama_Modul', $global_module_name)->get() as $modulwa_config) {
		    $cfgnegara = $modulwa_config->suiten_countrycode;
			$cfgprefix = $modulwa_config->suiten_countryprefix;
			$cfgmobile = $modulwa_config->suiten_mobileprefix;
		}
		if ($cfgprefix <> "") 
			$amobiles = explode(";",$cfgmobile);

		//logactivity("Negara ".$cfgnegara);
		//logactivity("Prefix ".$cfgprefix);
		//logactivity("Mobile ".$cfgmobile);
		$lokasi_listnegara = dirname(dirname(dirname(__DIR__))) . '/resources/country/dist.countries.json';
		// LogActivity($lokasi_listnegara);
		$filenegara = file_get_contents( $lokasi_listnegara );
		$datanegara = json_decode($filenegara, true);

		$negara = ($negara == "" ? strtoupper($cfgnegara) : strtoupper($negara));
		$CekNomor = ltrim(preg_replace('/\D/', '', $inputnomor), 0);
		$tidakadakodenegara  = (strpos($inputnomor, '+') === false);
					
		if ($inputnomor[0] == "+") {
			$CekNomor = $CekNomor;
		} elseif ($negara == $cfgnegara) {
			if ($cfgprefix <> "") {
				$NomorUbahan = $CekNomor;
				foreach ($amobiles as $prefixmobile) {
					if ($prefixmobile <> "") {
						if (substr($CekNomor,0,strlen($cfgprefix)*2) == $cfgprefix.$cfgprefix) {
							$NomorUbahan = substr($CekNomor,strlen($cfgprefix));
						} elseif  (substr($CekNomor,0,strlen($prefixmobile)) == $prefixmobile) {
							if ($inputnomor[0]<>"+")
								$NomorUbahan = $cfgprefix.substr($CekNomor,strlen($prefixmobile)-1);
						} 				
					}
				}
				// Force country code if not exists
				if (substr($NomorUbahan,0,strlen($cfgprefix)) <> $cfgprefix) {
					$NomorUbahan = $cfgprefix.$NomorUbahan;
				}
				$CekNomor = $NomorUbahan;
			}
			// else ... leave it as is
		} else {
			$prefixnegara = $datanegara[$negara]['callingCode'];
			if( substr($CekNomor,0,strlen($prefixnegara)) == $prefixnegara ) {
				$CekNomor = $CekNomor;
			} else {
				if ($tidakadakodenegara) {
					$CekNomor = $prefixnegara . $CekNomor;
				} else {
					$CekNomor = $CekNomor;
				}
			}
		}
		
		// special arrangement for mexico
		if (substr($CekNomor,0,2) == '52' && substr($CekNomor,2,1) !== '1') {
			$CekNomor = '521' . substr($CekNomor,2);
		}
		
		return $CekNomor;
	}
	
}

if (!function_exists("suiten_pilihbahasatemplate")) {

    function suiten_pilihbahasatemplate($negara, $inputnomor, $templatelokal, $templateenglish)
	{
		$global_module_name = 'Hostinoz Whatsapp API';

		// Ambil Data Config
		foreach (WHMCS\Database\Capsule::table('s10wa_config')->WHERE('suiten_Nama_Modul', $global_module_name)->get() as $modulwa_config) {
		    $cfgnegara = $modulwa_config->suiten_countrycode;
			$cfgprefix = $modulwa_config->suiten_countryprefix;
			$cfgmobile = $modulwa_config->suiten_mobileprefix;
		}
			
		$negara = ($negara == "" ? strtoupper($cfgnegara) : strtoupper($negara));
		$CekNomor = ltrim(preg_replace('/\D/', '', $inputnomor), 0);
		$templateenglish = ($templateenglish == "" ? $templatelokal : $templateenglish);

		if (substr($CekNomor, 0, strlen($cfgprefix)) == $cfgprefix) {
			$templatenya = $templatelokal;
		} else {
			$templatenya = $templateenglish;
		}	

		return $templatenya;
	}
	
}
	
?>