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