import Bluebird from"../deps/bluebird_fork.js";import{ProgramInspector,canonicalizeLocale}from"../definition/ProgramInspector.js";import{getCachedFormElements}from"../execution/ActivityLoader.js";import{Activator}from"../Activator.js";import{run}from"./ProgramRunner.js";import{check}from"../licensing/LicenseChecker.js";import{getStringsForProgram}from"../LanguageStringUtils.js";import{Task}from"../Task.js";class Engine{constructor(e){this._getFormElementRegistration=e=>this.formElements[e],this._registerFormElement=e=>{var t,r=e["id"];this.formElements[r]?(t={...this.formElements[r],...e},this.formElements[r]=t):this.formElements[r]=e},this.activator=new Activator,this.environment={},this.trivia={getFormElementRegistration:this._getFormElementRegistration,registerFormElement:this._registerFormElement},this.factories=[],this.implementations={},this.formElements={...getCachedFormElements()},this.appSkuCode=e}static buildOptions(e,t){(t={...t||{}}).locale=canonicalizeLocale(t.locale),t.locale||(t.locale=e.getDefaultLocale());const r=getStringsForProgram(e,t);return t.environment={encodeHtml:function(e){return"string"==typeof e?e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;"):""},getText:function(e){if("string"==typeof e&&"@"===e.charAt(0)){var t=e.slice(1);if(t in r)return r[t]}return e},strings:r,...t.environment||{}},t}createHandler(e,t){var r=this.implementations[e];if(void 0!==r){const o=Bluebird.resolve(r);return o.isFulfilled()?new(o.value()):o.then(function(e){return Bluebird.attempt(function(){return new e})})}for(const n of this.factories){var i=n.create(e,this._registerFormElement,t);if(void 0!==i)return i}}run(r,i){const o=this;return Task.attempt(function(){let e;r instanceof ProgramInspector?e=r:(e=new ProgramInspector,e.import(r)),check(e.program,o.appSkuCode),i=Engine.buildOptions(e,i);var t=e.resolveStart(i?i.start:void 0);return run(o,e,i,t)})}}export{Engine};