"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&!("get"in i?!t.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,o,i)}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const path_1=__importDefault(require("path")),fs_extra_1=__importDefault(require("fs-extra")),Module_1=__importDefault(require("../../common/Module")),flow_1=__importDefault(require("../../common/flow")),Error_1=__importStar(require("../../common/Error")),sniffer_1=require("../../protocol/sniffer"),protocol_1=require("../../types/protocol"),simulator_1=require("../../types/simulator"),i18n_1=__importDefault(require("../../i18n"));class SnifferModule extends Module_1.default{constructor(){super(protocol_1.ModuleName.sniffer),this.dataMap=new Map}[sniffer_1.CmdEnum.GetAppType](e,t=!0){const r=this.log.bind(this,"info",sniffer_1.CmdEnum.GetAppType),o=this.dataMap.get(e);return o&&t?Promise.resolve(o.appType):new Promise((t,o)=>{const i=flow_1.default.appTypes,n=[];r("getAppType","start"),Promise.race(i.map(t=>flow_1.default.run(t,"isApp",e).then(e=>e===simulator_1.CompilePathType.unknown?new Promise((e,t)=>{n.push(e),n.length===i.length&&t(new Error_1.default({code:Error_1.CoreErrorCode.COMPILE_PATH_ERROR,message:i18n_1.default.t("COMPILE_PATH_ERROR")}))}):{appType:t,compileType:e}))).then(o=>{r("getAppType","end",{data:o}),n.forEach(e=>e(o)),this.dataMap.set(e,o),t(o.appType),flow_1.default.run(o.appType,"init",e)}).catch(e=>{r("getAppType","end",{error:e}),n.forEach(e=>e(null)),o(new Error_1.default({code:Error_1.CoreErrorCode.COMPILE_PATH_ERROR,message:e&&e.message||i18n_1.default.t("COMPILE_PATH_ERROR_PATH_INVALID")}))})})}[sniffer_1.CmdEnum.GetAppTypeSync](e){const t=this.dataMap.get(e);return t&&t.appType}[sniffer_1.CmdEnum.GetAppCompileType](e){const t=this.dataMap.get(e);return t&&t.compileType}[sniffer_1.CmdEnum.ResetCompilePath](e,t){if(e===t)return;const r=this.dataMap.get(e);r&&(this.dataMap.delete(e),t&&this.dataMap.set(t,r))}[sniffer_1.CmdEnum.GetFrameworkOption](e){const t=this.log.bind(this,"info",sniffer_1.CmdEnum.GetFrameworkOption),r={useUpgrade:!1,minimumVersion:"5.12"};try{if(this[sniffer_1.CmdEnum.GetAppCompileType](e)===simulator_1.CompilePathType.sourceCode){t("getFrameworkOption","startReadOption");const{miniprogramRoot:o="."}=fs_extra_1.default.readJSONSync(path_1.default.join(e,"project.config.json"));if(t("getFrameworkOption","miniprogramRoot",o),!fs_extra_1.default.pathExistsSync(path_1.default.join(e,o,"app.json")))return r;const{newFramework:i}=fs_extra_1.default.readJSONSync(path_1.default.join(e,o,"app.json"));t("getFrameworkOption","newFramework",i),!0===(null==i?void 0:i.enabled)&&(r.useUpgrade=!0,r.minimumVersion=/^\d+\.\d+$/.test(i.minimumSupportedVersion)?i.minimumSupportedVersion:"5.12")}}catch(e){t("getFrameworkOption","err",e)}return t("getFrameworkOption","end",r),r}}exports.default=SnifferModule;