/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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 i,isPresetTemplateDefinition as o,isFeatureTemplateDefinition as s}from"./sharedTemplates/templateDefinitions/templateDefinitionUtils.js";import{isSubtypeGroupLayer as a,isSubtypeSublayer as l,isFeatureLayer as p}from"../layers/support/layerUtils.js";import{querySharedTemplates as u}from"../rest/sharedTemplates/querySharedTemplates.js";import{getServices as f}from"../undoredo/support/Services.js";const c=()=>e.getLogger("esri.editing.templateUtils");function y(e){return null!=e&&"prototype"in e}function m(e){return g(e)&&!("definition"in e)}function d(e){return g(e)&&"definition"in e}function g(e){return null!=e&&"templateId"in e}function w(e){return d(e)&&"feature"===e?.type&&(null==e.definition||s(e.definition))}function A(e){return d(e)&&"group"===e?.type&&(null==e.definition||i(e.definition))}function h(e){return d(e)&&"preset"===e?.type&&(null==e.definition||o(e.definition))}function j(e){return d(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:i}){if(null==t)return e.map(e=>({layer:e,templates:v(e)}));const o=await M(e,t);r(n);const s=new Map,a=Array.from(o).map(async e=>(await e.load(),await b({serviceInfo:e,out:s,signal:n,queryOptions:i}))),l=await Promise.allSettled(a);for(const r of l.filter(e=>"rejected"===e.status))c().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 b({serviceInfo:e,out:t,signal:n,queryOptions:i}){const{featureService:o,layersAndTables:s}=e,l=await q({featureService:o,layers:s.toArray(),signal:n,queryOptions:i});r(n);for(const[r,p]of l.entries())if(0!==p.length)if(a(r)){const e=O(p),n=[],i=e.get(1/0)??n;for(const o of r.sublayers){const r=e.get(o.subtypeCode)??n;t.set(o,[...r,...i])}}else t.set(r,p)}async function q({featureService:e,layers:r,signal:n,queryOptions:i}){if(!e.capabilities?.editing.supportsSharedTemplates)return new Map;const o=new Map(r.map(e=>[e.layerId,e])),s=await u({featureService:e,query:{layers:Array.from(o.keys()),...i},requestOptions:{signal:n}}),a=new Map;for(const l of s)for(const e of l.layerIds)o.has(e)&&t(a,o.get(e),()=>[]).push(l);return a}async function M(e,t){const r=f(t),i=new Set;for(const o of e)n(i,L(r,o));return await Promise.all(Array.from(i).map(e=>e.load())),i}function L(e,t){return l(t)&&t.parent?e.tablesAndLayersLookup.get(t.parent):p(t)?e.tablesAndLayersLookup.get(t):null}function O(e){const r=new Map;for(const n of e)t(r,n.subtypeCode??1/0,()=>[]).push(n);return r}export{v as getAllStandardFeatureTemplatesForLayer,L as getServiceInfoForLayer,S as getTemplatesForLayers,j as isLoadedSharedTemplate,w as isSharedFeatureTemplate,A as isSharedGroupTemplate,h as isSharedPresetTemplate,d as isSharedTemplate,m as isSharedTemplateMetadata,g as isSharedTemplateOrMetadata,y as isStandardFeatureTemplate};
