import Bluebird from"./deps/bluebird_fork.js";import esriRequest from"@arcgis/core/request";import{isArcGisApiVersion3x}from"./activities/arcgis/ArcGisApiVersion.js";import{ProgramInspector}from"./definition/ProgramInspector.js";import{Dictionary}from"./Collections.js";class ProgramInspectorFacility{constructor(){this._cache=new Dictionary}resolveInspector(o,i=!0,s){return Bluebird.attempt(()=>{if(!o)return this.downloadAndResolveInspector(void 0,s);var r="string"==typeof o?o:o.program||o.url;if(i){const t="string"==typeof o?o:o.id||o.url;if(void 0!==t&&null!==t){var e=this._cache.find(t);return void 0!==e?e:(this._cache.assign(t,this.downloadAndResolveInspector(r,s).catch(r=>(this._cache.remove(t),Bluebird.reject(r)))),this._cache.find(t))}}return this.downloadAndResolveInspector(r,s)})}downloadAndResolveInspector(r,e){if(r instanceof ProgramInspector)return Bluebird.resolve(r);if("object"==typeof r)return this.createInspector(r);const t=e||this.loadWorkflow;return t(r).then(this.createInspector).catch(r=>Bluebird.reject(r))}loadWorkflow(r){var e=r.match(/^(.*?)\/home\/item\.html\?id=(.*?)(\#.*)?$/);if(null!=e&&(r=`${e[1]}/sharing/rest/content/items/${e[2]}/data?f=json`),isArcGisApiVersion3x){e={url:r};return Bluebird.resolve(esriRequest(e))}return Bluebird.resolve(esriRequest(r)).then(r=>Bluebird.resolve(r.data))}createInspector(e){return Bluebird.attempt(function(){if(e instanceof ProgramInspector)return e;const r=new ProgramInspector;return r.import(e),r})}}export{ProgramInspectorFacility};