/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../core/maybe","../DimensionalDefinition"],(function(e,n,t){"use strict";function a(e,n,t){const i=n.shift();if(0===t.length){const e=[];t.push({sliceId:-1,multidimensionalDefinition:e})}const r=t.length;for(let a=0;a<r;a++){const n=t.shift().multidimensionalDefinition;i.values.forEach((a=>{t.push({sliceId:-1,multidimensionalDefinition:[...n,{variableName:e,dimensionName:i.name,values:[a]}]})}))}n.length&&a(e,n,t)}function i(e,n){const t=[];let i=0;return(n?e.variables.filter((e=>e.name.toLowerCase()===n.toLowerCase())):[...e.variables].sort(((e,n)=>e.name>n.name?1:-1))).forEach((e=>{const n=[],r=[...e.dimensions].sort(((e,n)=>e.name>n.name?-1:1));a(e.name,r,n),n.forEach((e=>{t.push({...e,sliceId:i++})}))})),t}function r(e,t,a){let i=e;if(t&&(t=[...t].sort(((e,n)=>e.dimensionName<n.dimensionName?-1:1))).forEach((({dimensionName:e,values:n,isSlice:t})=>{n.length&&(i=i.filter((a=>{const i=a.multidimensionalDefinition.find((n=>n.dimensionName===e));if(null==i)return!1;const r=i.values[0];return"number"==typeof r?"number"==typeof n[0]?n.includes(r):n.some((e=>e[0]<=r&&e[1]>=r)):"number"==typeof n[0]?n.some((e=>r[0]<=e&&r[1]>=e)):t?n.some((e=>e[0]===r[0]&&e[0]===r[1])):n.some((e=>e[0]>=r[0]&&e[0]<=r[1]||e[1]>=r[0]&&e[1]<=r[1]||e[0]<r[0]&&e[1]>r[1]))})))})),i.length&&a&&n.isSome(a.start)&&n.isSome(a.end)){const e=a.start.getTime(),n=a.end.getTime(),t=i[0].multidimensionalDefinition.findIndex((e=>"StdTime"===e.dimensionName));t>-1&&(i=i.filter((a=>{const i=a.multidimensionalDefinition[t].values[0];return e<=i&&n>=i})))}return i.map((e=>e.sliceId))}function s(e,a){var i;const{multidimensionalInfo:r,keyProperties:s}=e;if(n.isNone(r))return null;const l=null==s?void 0:s.DefaultVariable,{variables:o}=r,u=l&&null!=(i=o.find((({name:e})=>e===l)))?i:o[0];return u.dimensions.map((e=>{var n;const{values:i,extent:r}=e;let s=null!=(n=null==i?void 0:i[0])?n:r[0];return"stdz"===e.name.toLowerCase()&&!e.hasRanges&&Math.abs(r[1])<=Math.abs(r[0])&&(s=null!=i&&i.length?e.values[i.length-1]:r[1]),new t({variableName:u.name,dimensionName:e.name,values:[s],isSlice:!a||!!e.hasRanges})}))}function l(e){return!(n.isNone(e)||!e.length)&&e.some((e=>{if(null==e.values)return!0;const n=e.values.length;return 0===n||n>1||!e.isSlice&&Array.isArray(e.values[0])}))}function o(e,n){var t;const{values:a}=n;if(null!=a&&a.length)return Array.isArray(a[0])!==Array.isArray(e)?-1:Array.isArray(a[0])?a.findIndex((n=>n[0]===e[0]&&n[1]===e[1])):a.indexOf(e);const{extent:i}=n;if(Array.isArray(e)||e<i[0]||e>i[1])return-1;const r=n.interval||1;if("ISO8601"!==n.unit)return Math.round((e-i[0])/r);const s=i[0];let l=-1;switch((null==(t=n.intervalUnit)?void 0:t.toLowerCase())||"seconds"){case"seconds":l=Math.round((e-s)/1e3/r);break;case"minutes":l=Math.round((e-s)/6e4/r);break;case"hours":l=Math.round((e-s)/36e5/r);break;case"days":l=Math.round((e-s)/864e5/r);break;case"months":{const n=new Date(e).getUTCFullYear()-new Date(s).getUTCFullYear(),t=new Date(s).getUTCMonth(),a=new Date(e).getUTCMonth();l=0===n?a-t:a+11-t+12*(n-1)}break;case"years":l=Math.round((new Date(e).getUTCFullYear()-new Date(s).getUTCFullYear())/r);break;case"decades":l=Math.round((new Date(e).getUTCFullYear()-new Date(s).getUTCFullYear())/10/r)}return l}function u(e){var n,t,a;let i=null==(n=e.values)?void 0:n.length;if(i)return i;const{extent:r,unit:s}=e,l=e.interval||1,o=r[1]-r[0];if("ISO8601"!==s)return Math.round(o/l);switch(null!=(t=null==(a=e.intervalUnit)?void 0:a.toLowerCase())?t:"seconds"){case"seconds":i=Math.round(o/1e3/l);break;case"minutes":i=Math.round(o/6e4/l);break;case"hours":i=Math.round(o/36e5/l);break;case"days":i=Math.round(o/864e5/l);break;case"months":{const e=new Date(r[1]).getUTCFullYear()-new Date(r[0]).getUTCFullYear(),n=new Date(r[1][0]).getUTCMonth(),t=new Date(r[1][1]).getUTCMonth();i=0===e?t-n+1:t+11-n+12*(e-1)+1}break;case"years":i=Math.round((new Date(r[1]).getUTCFullYear()-new Date(r[0]).getUTCFullYear())/l);break;case"decades":i=Math.round((new Date(r[1]).getUTCFullYear()-new Date(r[0]).getUTCFullYear())/10/l);break;default:i=0}return i}function c(e,n){let t=0;const a=e[0].variableName,i=[...n.variables].sort(((e,n)=>e.name>n.name?1:-1));for(let r=0;r<i.length;r++){const n=i[r],s=[...n.dimensions].sort(((e,n)=>e.name>n.name?-1:1));if(n.name!==a){t+=s.map((e=>u(e))).reduce(((e,n)=>e*n));continue}const l=s.map((e=>u(e))),c=s.length;for(let a=0;a<c;a++){const n=e.find((e=>e.dimensionName===s[a].name));if(null==n)return null;const i=o(n.values[0],s[a]);if(-1===i)return null;l.shift(),t+=a===c-1?i:i*l.reduce(((e,n)=>e*n))}break}return t}e.createSlices=i,e.getDefaultMultidimensionalDefinition=s,e.getSliceIds=r,e.getSliceIndex=c,e.isMultiSliceOrRangeDefinition=l,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
