/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../core/Logger.js";import{getOrCreateMapValue as t}from"../core/MapUtils.js";import{throwIfAborted as r}from"../core/promiseUtils.js";import{addMaybe as n}from"../core/SetUtils.js";import{isGroupTemplateDefinition as o,isPresetTemplateDefinition as i,isFeatureTemplateDefinition as s}from"./sharedTemplates/templateDefinitions/templateDefinitionUtils.js";import{isFieldVisibleByDefault as a}from"../layers/support/fieldUtils.js";import{isSubtypeGroupLayer as l,isSubtypeSublayer as f,isFeatureLayer as u}from"../layers/support/layerUtils.js";import{querySharedTemplates as p}from"../rest/sharedTemplates/querySharedTemplates.js";import{getServices as c}from"../undoredo/support/Services.js";const y=()=>e.getLogger("esri.editing.templateUtils");function m(e){return null!=e&&"prototype"in e}function d(e){return w(e)&&!("definition"in e)}function g(e){return w(e)&&"definition"in e}function w(e){return null!=e&&"templateId"in e}function h(e){return g(e)&&"feature"===e?.type&&(null==e.definition||s(e.definition))}function j(e){return g(e)&&"group"===e?.type&&(null==e.definition||o(e.definition))}function b(e){return g(e)&&"preset"===e?.type&&(null==e.definition||i(e.definition))}function A(e){return g(e)&&null!=e?.definition}const v=e=>{if(null==e||"object"!=typeof e)return[];return[..."templates"in e&&Array.isArray(e.templates)?e.templates:[],..."types"in e&&Array.isArray(e.types)?e.types.flatMap(e=>e.templates):[]]};async function S({layers:e,view:t,signal:n,queryOptions:o}){if(null==t)return e.map(e=>({layer:e,templates:v(e)}));const i=await O(e,t);r(n);const s=new Map,a=Array.from(i).map(async e=>(await e.load(),await q({serviceInfo:e,out:s,signal:n,queryOptions:o}))),l=await Promise.allSettled(a);for(const r of l.filter(e=>"rejected"===e.status))y().warn("Failed to fetch shared templates for service. Will use standard templates if available.",r.reason);return e.map(e=>({layer:e,templates:s.get(e)??v(e)}))}async function q({serviceInfo:e,out:t,signal:n,queryOptions:o}){const{featureService:i,layersAndTables:s}=e,a=await M({featureService:i,layers:s.toArray(),signal:n,queryOptions:o});r(n);for(const[r,f]of a.entries())if(0!==f.length)if(l(r)){const e=L(f),n=[],o=e.get(1/0)??n;for(const i of r.sublayers){const r=e.get(i.subtypeCode)??n;t.set(i,[...r,...o])}}else t.set(r,f)}async function M({featureService:e,layers:r,signal:n,queryOptions:o}){if(!e.capabilities?.editing.supportsSharedTemplates)return new Map;const i=new Map;for(const l of r)t(i,l.layerId,()=>[]).push(l);const s=await p({featureService:e,query:{layers:Array.from(i.keys()),...o},requestOptions:{signal:n}}),a=new Map;for(const l of s)for(const e of l.layerIds)if(i.has(e))for(const r of i.get(e))t(a,r,()=>[]).push(l);return a}async function O(e,t){const r=c(t),o=new Set;for(const i of e)n(o,U(r,i));return await Promise.all(Array.from(o).map(e=>e.load())),o}function U(e,t){return f(t)&&t.parent?e.tablesAndLayersLookup.get(t.parent):u(t)?e.tablesAndLayersLookup.get(t):null}function L(e){const r=new Map;for(const n of e)t(r,n.subtypeCode??1/0,()=>[]).push(n);return r}function I(e,t={},r){if(j(e)||b(e))return{};const n=new Map;r?.fields.forEach(e=>{null!=e.defaultValue&&a(e,r)&&n.set(e.name,e.defaultValue)});const o=Object.fromEntries(n);return m(e)?{...o,...e.prototype.attributes,...t}:h(e)?{...o,...e.definition?.defaultValues,...t}:{...o,...t}}export{v as getAllStandardFeatureTemplatesForLayer,I as getCreationAttributes,U as getServiceInfoForLayer,S as getTemplatesForLayers,A as isLoadedSharedTemplate,h as isSharedFeatureTemplate,j as isSharedGroupTemplate,b as isSharedPresetTemplate,g as isSharedTemplate,d as isSharedTemplateMetadata,w as isSharedTemplateOrMetadata,m as isStandardFeatureTemplate};
