/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{isSome as e}from"../../../core/arrayUtils.js";import{createFieldInfos as i}from"../../../support/popupUtils.js";import t from"../../../tables/AttributeTableTemplate.js";import l from"../../../tables/elements/AttributeTableAttachmentElement.js";import n from"../../../tables/elements/AttributeTableFieldElement.js";import r from"../../../tables/elements/AttributeTableGroupElement.js";import o from"../../../tables/elements/AttributeTableRelationshipElement.js";import s from"../../../tables/support/FieldOrder.js";import a from"./AttachmentsColumnTemplate.js";import{isGroupColumn as d,isFieldColumn as m,isRelationshipColumn as p,isAttachmentsColumn as f}from"./columnUtils.js";import u from"./FieldColumnTemplate.js";import c from"./GroupColumnTemplate.js";import h from"./RelationshipColumnTemplate.js";import b from"./TableTemplate.js";import{isIFeatureTableSupportedLayer as w,isIFeatureTableSupportedLayerWithAttachments as y,isIFeatureTableSupportedLayerWithRelationships as T,hasTemplateForField as j}from"./tableUtils.js";function F(i,t){const l=[];return i?.forEach(i=>{const{description:n,label:r,type:o}=i;if("group"===o){const o=i.elements.map(e=>v(e,t)).filter(e);if(!o?.length)return;l.push(new c({description:n,columnTemplates:o,label:r}))}else{const e=v(i,t);if(!e)return;l.push(e)}}),l}function v(e,i){const{description:t,label:l,type:n}=e;if("field"===n){const n=e.fieldName??void 0,r=i.findIndex(e=>e.field&&e.field===n),o=r>-1?i.at(r)?.order:void 0;return new u({description:t,direction:o,fieldName:n,initialSortPriority:o&&r>-1?r:null,label:l})}return"attachment"===n?new a({description:t,label:l}):"relationship"===n?new h({description:t,label:l,relationshipId:e.relationshipId}):void 0}function N(i){const t=[];return i.forEach(i=>{const{hidden:l}=i;if(d(i)){if(l)return;const n=i.columns?.map(e=>E(e)).filter(e);if(!n?.length)return;t.push(new r({elements:n}))}else{const e=E(i);if(!e)return;t.push(e)}}),t}function E(e){const{fieldName:i,hidden:t}=e;if(m(e)){if(!t)return new n({fieldName:i})}else if(p(e)){if(!t)return new o({relationshipId:e.relationshipId})}else if(f(e)&&!t)return new l}async function I({layer:e,excludeAttachments:r,excludeRelationships:s,excludedFieldTypesOverride:a}){const d=[],m=[];if(!e||!w(e))return new t;await e.load();const p=i(e,{...a&&{ignoreFieldTypes:a},sortDisabled:!0});for(const{visible:i,fieldName:t}of p)i&&d.push(new n({fieldName:t}));return!d.length&&e.objectIdField&&d.push(new n({fieldName:e.objectIdField})),y(e)&&!0!==r&&d.push(new l),T(e)&&!0!==s&&e.relationships?.forEach(e=>{d.push(new o({relationshipId:e.id}))}),new t({elements:d,orderByFields:m})}async function x(e){const{excludeAttachments:t,excludeRelationships:l,layer:n,excludedFieldTypesOverride:r}=e,o=[];await n.load();return i(n,{...r&&{ignoreFieldTypes:r},sortDisabled:!0}).forEach(({fieldName:e,format:i,label:t,visible:l})=>{null!=e&&o.push(new u({fieldName:e,format:i,label:t,visible:l}))}),y(n)&&!0!==t&&o.push(new a),T(n)&&!0!==l&&n.relationships?.forEach(({id:e})=>{o.push(new h({relationshipId:e}))}),new b({columnTemplates:o})}async function A(e){const{layer:t,template:l,includeHiddenFields:n,excludedFieldTypesOverride:r}=e,o=l.orderByFields??[],s=F(l.elements,o);if(n){await t.load();i(t,{...r&&{ignoreFieldTypes:r},sortDisabled:!0}).forEach(e=>{if(!e.fieldName)return;const{fieldName:i}=e;if(!j(i,s)){const e=o.findIndex(e=>e.field&&e.field===i),t=e>-1?o.at(e)?.order:void 0,l=t&&e>-1?e:null;s.push(new u({direction:t,fieldName:i,initialSortPriority:l,visible:!1}))}}),y(t)&&!s.some(e=>"attachment"===e.type)&&s.push(new a({visible:!1})),T(t)&&t.relationships?.forEach(({id:e})=>{s.some(i=>"relationship"===i.type&&i.relationshipId===e)||s.push(new h({relationshipId:e,visible:!1}))})}return new b({columnTemplates:s})}function g(e,i,t=!0){const{allColumns:l,columns:n}=e,r=[],o=N(n.toArray());e.activeSortOrders.forEach(({fieldName:e,direction:i})=>{e&&i&&r.push(new s({field:e,order:i}))});return l.filter(e=>null!=e.direction&&(!e.hidden||t)).forEach(({fieldName:e,direction:i})=>{const t=r.find(i=>i.field===e);i&&!t&&r.push(new s({field:e,order:i}))}),i.elements=o,i.orderByFields=r,i}export{N as createAttributeTableElements,I as createAttributeTableTemplateFromLayer,F as createColumnTemplates,E as createNestedAttributeTableElement,v as createNestedColumnTemplate,A as createTableTemplateFromAttributeTableTemplate,x as createTableTemplateFromLayer,g as syncAttributeTableTemplateWithTable};
