/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../core/Error.js";import i from"../../core/Logger.js";import{isSerializable as t}from"../../core/support/jsonUtils.js";import{lowerCaseNormalizedFieldName as s,applyLowerCaseFieldName as n,isDateField as l,isNumericField as r,isObjectIDField as d,isGlobalIDField as o,getFieldDefaultValue as a,getFieldFromIndex as m,isTimeOnlyField as u}from"./fieldUtils.js";import{utc as f,unknown as F}from"../../time/constants.js";import{fromTimeReference as h}from"../../time/timeZoneUtils.js";function p(e){return"timeZone"in e}function c(e){return"timeZone"in e}function y(e){return"dateFieldsTimeZone"in e}class _{static fromJSON(e){return new _(e.fields,e.timeZoneByFieldName)}static fromLayer(e){return new _(e.fields??[],T(e))}static fromLayerJSON(e){return new _(e.fields??[],T(e))}constructor(e=[],i){this._fieldsMap=new Map,this._normalizedFieldsMap=new Map,this._dateFieldsSet=new Set,this._numericFieldsSet=new Set,this._requiredFields=null,this.dateFields=[],this.numericFields=[],this.fields=e||[],this._timeZoneByFieldName=i?new Map(i):null;const t=[];for(const a of this.fields){const e=a?.name,i=s(e);if(e&&i){const s=n(e);this._fieldsMap.set(e,a),this._fieldsMap.set(s,a),this._normalizedFieldsMap.set(i,a),t.push(`${s}:${a.type}:${this._timeZoneByFieldName?.get(e)}`),l(a)?(this.dateFields.push(a),this._dateFieldsSet.add(a)):r(a)&&(this._numericFieldsSet.add(a),this.numericFields.push(a)),d(a)||o(a)||(a.editable=null==a.editable||!!a.editable,a.nullable=null==a.nullable||!!a.nullable)}}t.sort(),this.uid=t.join()}get requiredFields(){if(!this._requiredFields){this._requiredFields=[];for(const e of this.fields)d(e)||o(e)||e.nullable||void 0!==a(e)||this._requiredFields.push(e)}return this._requiredFields}equals(e){return this.uid===e?.uid}has(e){return null!=this.get(e)}get(e){return m({fieldName:e,fieldsMap:this._fieldsMap,normalizedFieldsMap:this._normalizedFieldsMap})}getTimeZone(t){const s=this.get(t&&"string"!=typeof t?t.name:t);return s?this._timeZoneByFieldName?this._timeZoneByFieldName.get(s.name):"date"===s.type||"esriFieldTypeDate"===s.type?(i.getLogger("esri.layers.support.FieldsIndex").errorOnce(new e("getTimeZone:no-timezone-information",`no time zone information for field '${s.name}'`)),f):g.has(s.type)?F:null:null}isDateField(e){return this._dateFieldsSet.has(this.get(e))}isTimeOnlyField(e){return u(this.get(e))}isNumericField(e){return this._numericFieldsSet.has(this.get(e))}normalizeFieldName(e){return this.get(e)?.name??void 0}toJSON(){return{fields:this.fields.map(e=>t(e)?e.toJSON():e),timeZoneByFieldName:this._timeZoneByFieldName?Array.from(this._timeZoneByFieldName.entries()):null}}}const g=new Set(["time-only","date-only","timestamp-offset","esriFieldTypeDateOnly","esriFieldTypeTimeOnly","esriFieldTypeTimestampOffset"]);function T(e){const i=new Map;if(!e.fields)return i;const t=!0===e.datesInUnknownTimezone,{timeInfo:s,editFieldsInfo:l}=e,r=(s?"startField"in s?s.startField:s.startTimeField:"")??"",d=(s?"endField"in s?s.endField:s.endTimeField:"")??"",o=y(e)?e.dateFieldsTimeZone??null:e.dateFieldsTimeReference?h(e.dateFieldsTimeReference):null,a=l?p(l)?l.timeZone??o:l.dateFieldsTimeReference?h(l.dateFieldsTimeReference):o??f:null,m=s?c(s)?s.timeZone??o:s.timeReference?h(s.timeReference):o:null,u=new Map([[n(l?.creationDateField??""),a],[n(l?.editDateField??""),a],[n(r),m],[n(d),m]]);for(const{name:f,type:h}of e.fields)if(g.has(h))i.set(f,F);else if("date"!==h&&"esriFieldTypeDate"!==h)i.set(f,null);else if(t)i.set(f,F);else{const e=u.get(n(f??""))??o;i.set(f,e)}return i}export{_ as default};
