import{registerModuleName,importModule}from"./ModuleLoader.js";const activityCache={},modules={},formElementCache={};function getCachedFormElements(){return{...formElementCache}}const httpPrefix="http://",httpsPrefix="https://";function isHttp(t){return t.substr(0,httpPrefix.length)===httpPrefix||t.substr(0,httpsPrefix.length)===httpsPrefix}function importRef(t,e){if(isHttp(t)){var o=registerModuleName(t);return importModule(o+"/main",e,o)}return importModule(t+"main",e)}function exportIsFormElementRegistration(t){return!("object"!=typeof t||!t.component)}function exportIsActivity(t){if("function"!=typeof t)return!1;var e=t.prototype;return"object"==typeof e&&null!==e&&("function"==typeof e.execute&&("string"==typeof t.action&&"string"==typeof t.suite))}async function loadRef(t,e,o,r){var i=await importRef(e,{action:t,suite:o});if("object"==typeof i&&null!==i&&modules[e]!==i)for(const f in modules[e]=i){var n=i[f];exportIsActivity(n)?createRef(n.action,e,n.suite,r,n):exportIsFormElementRegistration(n)&&r(formElementCache[n.id]=n)}}function createRef(t,e,o,r,i){let n=activityCache[t];void 0===n&&(n=activityCache[t]=[]);for(const f of n)if(f.path===e&&f.suite===o)return void 0!==i&&(f.factory=i),f;const f={factory:i,path:e,suite:o};return n.push(f),void 0!==i?f.factory=i:f.promise=loadRef(t,e,o,r),f}async function resolveRef(t){for(const e of t)if(await e.promise,(e.promise=void 0)!==e.factory)return new e.factory}class ActivityLoader{constructor(){this.references=[]}create(f,c,t){if("string"==typeof f){let n;if(void 0!==t){const o=e(t.program.references);if(void 0!==o)return o}const o=e(this.references);return void 0!==o?o:void 0!==n?resolveRef(n):void 0;function e(t){for(const r of t){var e=r.path,o=r.suite;const i=createRef(f,e,o,c);if(void 0===n){if(void 0!==i.factory)return new i.factory;void 0!==i.promise&&(n=[i])}else n.push(i)}}}}}ActivityLoader.defaultReference={path:"@geocortex/workflow/runtime/activities/",suite:"gcx:wf:builtin"};export{getCachedFormElements,ActivityLoader};