import Bluebird from"../../deps/bluebird_fork.js";import{jsonToQueryObject}from"../../activities/core/converters.js";import{isArcGisApiVersion3x}from"./ArcGisApiVersion.js";import Extent from"@arcgis/core/geometry/Extent";import esriRequest from"@arcgis/core/request";function isAbortable(e){return"object"==typeof e.signal}function isCancellable(e){return null!=e&&"function"==typeof e.cancel}function schedulePromiseCancellation(e,t){!isAbortable(e.cancellationToken)&&isCancellable(t)&&e.cancellationToken.finally(()=>t.cancel())}function inline(e,t,n=void 0){const r=Bluebird.resolve(e);return r.isFulfilled()?t(r.value()):r.isRejected()&&n?n(r.reason()):r.then(t,n)}function addToPath(n,r){return n.replace(/([\#\?].*)?$/,function(e){const t=n.substr(0,n.length-e.length);return t.match(/\/+$/)?r+e:"/"+r+e})}function isExtent(e){return"number"==typeof e.xmin&&("number"==typeof e.xmax&&("number"==typeof e.ymin&&"number"==typeof e.ymax))}function polygonFromExtent(e,t){if(null!=e)return isExtent(e)?new t({rings:[[[e.xmin,e.ymin],[e.xmax,e.ymin],[e.xmax,e.ymax],[e.xmin,e.ymax]]],spatialReference:e.spatialReference}):e}const __request=esriRequest;function get(e,t,n,r){if(t.f="json",isArcGisApiVersion3x){var i={url:e,content:jsonToQueryObject(t),handleAs:"json",timeout:n};return __request(i)}const a={method:"get",query:jsonToQueryObject(t),responseType:"json",timeout:n,cacheBust:!1};return r&&(a.signal=r),__request(e,a).then(e=>e.data)}function fetch(e,t,n,r){return isArcGisApiVersion3x&&(t.__ts=(new Date).valueOf()),get(e,t,n,r)}function post(e,t,n,r){if(t.f="json",isArcGisApiVersion3x){var i={url:e,content:jsonToQueryObject(t),handleAs:"json",timeout:n};return __request(i,{usePost:!0})}const a={method:"post",query:jsonToQueryObject(t),responseType:"json",timeout:n,cacheBust:!1};return r&&(a.signal=r),__request(e,a).then(e=>e.data)}function isFeatureSet(e){return null!=e&&(!Array.isArray(e)&&!!Array.isArray(e.features))}function featuresToArray(e){return null==e?[]:e instanceof Array?e:isFeatureSet(e)?e.features:void 0!==e.attributes&&null!==e.attributes||void 0!==e.geometry&&null!==e.geometry?[e]:[]}function extentOf(e){let t=void 0;for(const r of e||[])if(r)if("point"===r.type){var n=r;void 0===t?t=pointToExtent(n):(t.xmin=Math.min(t.xmin,n.x),t.ymin=Math.min(t.ymin,n.y),t.xmax=Math.max(t.xmax,n.x),t.ymax=Math.max(t.ymax,n.y),t.spatialReference=r.spatialReference||t.spatialReference,"number"==typeof n.z&&(t.zmin=Math.min(t.zmin,n.z),t.zmax=Math.max(t.zmax,n.z)))}else{let e=isArcGisApiVersion3x?r.getExtent():r.extent;void 0!==e&&null!==e&&(void 0!==t?(t.xmin=Math.min(t.xmin,e.xmin),t.ymin=Math.min(t.ymin,e.ymin),t.xmax=Math.max(t.xmax,e.xmax),t.ymax=Math.max(t.ymax,e.ymax),t.spatialReference=r.spatialReference||t.spatialReference,"number"==typeof e.zmin&&(t.zmin=Math.min(t.zmin,e.zmin)),"number"==typeof e.zmax&&(t.zmax=Math.max(t.zmax,e.zmax))):(isArcGisApiVersion3x||(e=e.clone()),t=e))}return t}function pointToExtent(e){const t=new Extent;return t.xmin=e.x,t.ymin=e.y,t.xmax=e.x,t.ymax=e.y,t.spatialReference=e.spatialReference,"number"==typeof e.z&&(t.zmin=e.z,t.zmax=e.z),t}function toEsriUnit(e){switch(e&&e.toLowerCase()){case"feet":return"9002";case"kilometers":return"9036";case"meters":return"9001";case"miles":return"9093";case"nautical-miles":return"109012";case"yards":return"109001";default:return e}}export{isAbortable,isCancellable,schedulePromiseCancellation,inline,addToPath,polygonFromExtent,fetch,post,isFeatureSet,featuresToArray,extentOf,toEsriUnit};