/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"./DateTimeFieldFormat.js";import e from"./FieldFormat.js";import{isNumericField as n,isDateField as r,isDateOnlyField as o,isTimeOnlyField as a,isTimestampOffsetField as u}from"./fieldUtils.js";import i from"./NumberFieldFormat.js";import s from"../../popup/support/FieldInfoFormat.js";const l={base:e,key:"type",errorContext:"field-configuration-field-format",typeMap:{"date-time":t,number:i}};function c(t,e){const{format:n}=t;return e&&h(e)?n?m(n,e):M(e,{hour12:"always",useGrouping:"always"}):null}function m(t,e){const{dateFormat:n}=t;if(!e)return n?y(t):f(t);if(!h(e))return;const r={hour12:"always"};switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return d(t);case"single":case"double":return f(t);case"date":case"timestamp-offset":return n?y(t):M(e,r);case"date-only":return n?g(t):M(e);case"time-only":return n?p(t):M(e,r)}}function h(t){return n(t)||r(t)||o(t)||a(t)||u(t)}function d(t){return new i({useGrouping:t.digitSeparator?"always":"never"})}function f(t){const e=d(t),{places:n}=t;return e.minimumFractionDigits=n??0,e.maximumFractionDigits=n??2,e}function y(e){return new t({dateStyle:w(e),year:b(e),month:S(e),timeStyle:$(e),hour12:F(e)})}function g(e){return new t({dateStyle:w(e),year:b(e),month:S(e)})}function p(e){return new t({timeStyle:$(e),hour12:F(e)})}function w(t){const{dateFormat:e}=t;return e?e.startsWith("short-date")?"short":e.startsWith("day-short-month-year")?"medium":e.startsWith("long-month-day-year")?"long":e.startsWith("long-date")?"full":null:null}function F(t){return t.dateFormat?.endsWith("-24")?"never":"always"}function S(t){switch(t.dateFormat){case"short-month-year":return"short";case"long-month-year":return"long";default:return null}}function $(t){const{dateFormat:e}=t;return e?e.includes("short-time")?"short":e.includes("long-time")?"medium":null:null}function b(t){switch(t.dateFormat){case"short-month-year":case"long-month-year":case"year":return"numeric";default:return null}}function D(t,e){const n="number"===t.type,r="date-time"===t.type;if(!e)return n?G(t):r?j(t):null;if(h(e))switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return n?x(t):null;case"single":case"double":return n?G(t):null;case"date":case"timestamp-offset":return r?j(t):null;case"date-only":return r?v(t):null;case"time-only":return r?W(t):null}}function x(t){return new s({digitSeparator:"never"!==t.useGrouping})}function G(t){const e=x(t);return e.places=t.maximumFractionDigits,e}function j(t){let e;const n=k(t),r=C(t),o="never"===t.hour12?"24":null,a="numeric"===t.year?"year":null,u=I(t);return n&&r&&o?e=`${n}-${r}-${o}`:n&&r?e=`${n}-${r}`:n?e=n:a&&u?e=`${u}-${a}`:a&&(e=a),e?new s({dateFormat:e}):null}function v(t){let e;const n=k(t),r="numeric"===t.year?"year":null,o=I(t);return n?e=n:r&&o?e=`${o}-${r}`:r&&(e=r),e?new s({dateFormat:e}):null}function W(t){let e;const n=C(t),r="never"===t.hour12?"24":null;return n&&r?e=`short-date-${n}-${r}`:n&&(e=`short-date-${n}`),e?new s({dateFormat:e}):null}function k(t){switch(t.dateStyle){case"short":return"short-date";case"medium":return"day-short-month-year";case"long":return"long-month-day-year";case"full":return"long-date";default:return}}function C(t){return t.timeStyle&&("short"===t.timeStyle?"short-time":"long-time")}function I(t){switch(t.month){case"short":return"short-month";case"long":return"long-month";default:return}}function M(e,n){if(!h(e))return;const r=n?.useGrouping??"auto",o=n?.hour12??"auto";switch(e.type){case"integer":case"small-integer":case"big-integer":case"long":return new i({minimumFractionDigits:0,maximumFractionDigits:0,useGrouping:r});case"single":case"double":return new i({minimumFractionDigits:0,maximumFractionDigits:2,useGrouping:r});case"date":case"timestamp-offset":return new t({dateStyle:"short",timeStyle:"short",hour12:o});case"date-only":return new t({dateStyle:"short"});case"time-only":return new t({timeStyle:"short",hour12:o})}}export{M as defaultFieldFormat,c as fieldFormatFromFieldInfo,m as fieldFormatFromFieldInfoFormat,D as fieldInfoFormatFromFieldFormat,l as formatTypes,h as isFormattableField};
