/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../request.js";import t from"../../core/Error.js";import{queryAllJSON as r}from"../../layers/support/featureQueryAll.js";import{isSubtypeSublayer as o,isFeatureLayer as n}from"../../layers/support/layerUtils.js";import s from"../../portal/PortalItem.js";import u from"../../rest/support/FeatureSet.js";import{isGraphic as a}from"../../support/graphicInstanceUtils.js";function l(e,t,r="from"){const{fromRuleElement:o,viaRuleElement:n,toRuleElement:s}=y(e),u=[];switch(e.ruleType){case 2:case 3:"from"===r&&m(t,o,!1)?u.push(s):"to"===r&&m(t,s,!1)&&u.push(o);break;case 1:m(t,o,!0)?u.push(s):m(t,s,!0)&&u.push(o);break;case 4:s.networkSource?.usesGeometry||(m(t,o,!0)?u.push(s):m(t,s,!0)&&u.push(o));break;case 5:n&&(m(t,n,!0)?(o.networkSource?.usesGeometry||u.push(o),s.networkSource?.usesGeometry||u.push(s)):(!o.networkSource?.usesGeometry&&m(t,o,!0)||!s.networkSource?.usesGeometry&&m(t,s,!0))&&u.push(n));break;default:return[]}return u}function i(e,t,r){const{fromRuleElement:o,viaRuleElement:n,toRuleElement:s}=y(e);switch(e.ruleType){case 2:case 3:return c(t,r,o,s,!1,!1);case 1:return c(t,r,o,s,!0,!0);case 4:return!s.networkSource?.usesGeometry&&c(t,r,o,s,!0,!0);case 5:return!o.networkSource?.usesGeometry&&c(t,r,o,n,!0,!0)||!s.networkSource?.usesGeometry&&c(t,r,s,n,!0,!0);default:return!1}}function c(e,t,r,o,n,s){if(!r||!o)return!1;const u=m(e,r,s),a=m(t,o,s);if(n){const n=m(e,o,s),l=m(t,r,s);return u&&a||n&&l}return u&&a}function p(e,t){const r=e.terminal?.terminalId,o=t.terminalId;return null==r&&null==o||(1===r?null==o||1===o:r===o)}function m(e,t,r){const{assetGroupCode:o,assetTypeCode:n}=e;return("layerId"in e?e.layerId===t.networkSource?.layerId:e.networkSourceId===t.networkSource?.sourceId)&&(null==o||o===t.assetGroup?.assetGroupCode)&&(null==n||n===t.assetType?.assetTypeCode)&&(!r||!("terminalId"in e)||p(t,e))}function y(e){return{fromRuleElement:{networkSource:e.fromNetworkSource,assetGroup:e.fromAssetGroup,assetType:e.fromAssetType,terminal:e.fromTerminal},viaRuleElement:e.viaNetworkSource?{networkSource:e.viaNetworkSource,assetGroup:e.viaAssetGroup,assetType:e.viaAssetType,terminal:e.viaTerminal}:void 0,toRuleElement:{networkSource:e.toNetworkSource,assetGroup:e.toAssetGroup,assetType:e.toAssetType,terminal:e.toTerminal}}}function f(e){let t=null,r=null,n=null;if(a(e))t=d(e),[r,n]=w(e);else if(o(e)){t=e.parent?.layerId??null;const[o]=I(e);o===e.subtypeField&&(r=e.subtypeCode)}else t=e.layerId??null;return{layerId:t,assetGroupCode:r,assetTypeCode:n}}function d(e){const{sourceLayer:t}=e;let r;return n(t)?r=t.layerId:o(t)&&(r=t.parent?.layerId),r??null}function w(e){const[t,r]=I(e.sourceLayer);return[t?e.attributes[t]:null,r?e.attributes[r]:null]}function I(e){if(!e||!("fieldsIndex"in e))return[null,null];return[e.fieldsIndex.normalizeFieldName("assetGroup")??null,e.fieldsIndex.normalizeFieldName("assetType")??null]}async function k(e,t){const o=e.layers,n=e.layerInfos,s=e.returnGeometry||!1,a=e.outSpatialReference;await Promise.all(o.map(async e=>{await e.load()}));return(await Promise.all(o.map(async e=>{const o=n.find(t=>t.layerUrl===e.parsedUrl?.path);if(!o?.objectIds?.length)return{layer:e,featureSet:void 0};const l=e.createQuery();l.returnGeometry=s,l.outFields=o.outFields||["*"],l.outSpatialReference=a,l.gdbVersion=e.gdbVersion,l.objectIds=o.objectIds,t&&(l.where="1=1");const i=u.fromJSON(await r(e,l));return i.features.forEach(t=>{t.layer=t.sourceLayer=e}),{layer:e,featureSet:i}}))).filter(e=>void 0!==e.featureSet)}async function S(e,t){if("Utility Network Layer"===e){const{default:e}=await import("../UtilityNetwork.js");return new e({layerUrl:t})}return null}async function G(r){let o="portalItem"in r?r:{portalItem:r};!o.portalItem||o.portalItem instanceof s||(o={...o,portalItem:new s(o.portalItem)});const n=o.portalItem;if(await n.load(),"Feature Service"!==n.type)throw new t("portal:unknown-item-type","Unknown item type '${type}'",{type:n.type});const u=n.url,a=await e(u,{responseType:"json",query:{f:"json"}}),l="Network Layer";if(a.data.type?.includes(l))return S(a.data.type,u);if(a.data.layers){const e=a.data.layers.find(e=>e.type.includes(l));if(e){const t=`${u}/${e.id}`;return S(e.type,t)}}return null}export{c as doElementsMatchRule,i as doesRuleAllowAssociation,I as getAssetFieldNames,l as getCompatibleRuleElements,y as getElementsFromRule,w as getFeatureAssetCodes,d as getFeatureSourceLayerId,k as getFeaturesFromLayers,f as getRuleValues,m as isRuleElementMatch,p as isTerminalMatch,G as networkFromPortalItem};
