import{isIE}from"../BrowserUtils.js";import{parseLocale}from"../definition/ProgramInspector.js";const CURRENT_CENTURY_YEAR_CUTOFF=29,SUPPORTED_DATE_FNS_LOCALES=["cs","da","de","de-AT","en-AU","en-CA","en-GB","en-IN","en-NZ","en-US","en-ZA","fr","fr-CA","fr-CH","it","nb","nl","nl-BE","nn","pt","pt-BR"];let localeDateFormat="",dateFormatSeparator="";function isValidDate(e){return e instanceof Date&&!isNaN(e.valueOf())}function tryParseYear(e){if(/^\d+$/.test(e)&&!(4<e.length)){for(2===e.length&&(e=`${CURRENT_CENTURY_YEAR_CUTOFF>=parseInt(e,10)?20:19}${e}`);e.length<4;)e=`0${e}`;return e}}function tryParseMonth(e){if(/^\d+$/.test(e)&&!(2<e.length)){1===e.length&&(e=`0${e}`);var t=parseInt(e,10);if(!(t<1||12<t))return e}}function tryParseDay(e){if(/^\d+$/.test(e)&&!(2<e.length)){1===e.length&&(e=`0${e}`);var t=parseInt(e,10);if(!(t<1||31<t))return e}}function tryParseDateText(e){if(e){const r=(e=e.replace(/\\/g,"\\\\")).match(/(\d+)/g);if(r&&3===r.length){var e=getLocaleDateFormatSeparator(),t=getLocaleDateFormatTokenPosition("yyyy"),a=getLocaleDateFormatTokenPosition("MM");for(let e=0;e<3;e++){switch(e){case t:r[e]=tryParseYear(r[e]);break;case a:r[e]=tryParseMonth(r[e]);break;default:r[e]=tryParseDay(r[e])}if(void 0===r[e])return}return`${r[0]}${e}${r[1]}${e}${r[2]}`}}}async function getDateFnsLocale(e){const t=[];e=parseLocale(e);if(e){e.territory&&t.push(`${e.language}-${e.territory}`),t.push(e.language);for(const a of t)if(-1!==SUPPORTED_DATE_FNS_LOCALES.indexOf(a)){let e;try{e=await import(`date-fns/locale/${a}/index.min`)}catch{continue}if(null!==e&&void 0!==e&&e.localize)return e}}}function getLocaleDateFormat(){if(localeDateFormat)return localeDateFormat;const e=new Date(1999,11,31,0,0,0);return localeDateFormat=e.toLocaleDateString(void 0,{year:"numeric",month:"numeric",day:"numeric"}),localeDateFormat=localeDateFormat.replace("1999","yyyy"),localeDateFormat=localeDateFormat.replace("99","yyyy"),localeDateFormat=localeDateFormat.replace("12","MM"),localeDateFormat=localeDateFormat.replace("31","dd"),isIE()&&(localeDateFormat=localeDateFormat.replace(/[^ -~]/g,"")),localeDateFormat}function getLocaleDateFormatSeparator(){if(dateFormatSeparator)return dateFormatSeparator;const e=getLocaleDateFormat();return dateFormatSeparator=e[e.search(/[^dMy]/)],dateFormatSeparator}function getLocaleDateFormatTokenPosition(e){const t=getLocaleDateFormat();var a=t.indexOf("yyyy"),r=t.indexOf("MM"),o=t.indexOf("dd");return"yyyy"===e?0===a?0:r<a&&o<a?2:1:"MM"===e?0===r?0:o<r&&a<r?2:1:0===o?0:r<o&&a<o?2:1}function getDateAsLocaleFormattedString(e){if(!isValidDate(e))return"";var t=tryParseYear(e.getFullYear().toString()),a=tryParseMonth((e.getMonth()+1).toString()),e=tryParseDay(e.getDate().toString());let r=getLocaleDateFormat();return r=r.replace("yyyy",t),r=r.replace("MM",a),r=r.replace("dd",e),r}function __test__setlocaleDateFormatInCache(e){localeDateFormat=e,dateFormatSeparator=""}export{isValidDate,tryParseYear,tryParseMonth,tryParseDay,tryParseDateText,getDateFnsLocale,getLocaleDateFormat,getLocaleDateFormatSeparator,getLocaleDateFormatTokenPosition,getDateAsLocaleFormattedString,__test__setlocaleDateFormatInCache};