"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var s=Object.getOwnPropertyDescriptor(t,r);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,s)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=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 fs_1=__importDefault(require("fs")),path_1=__importDefault(require("path")),http_1=__importDefault(require("http")),ejs_1=__importDefault(require("ejs")),http_terminator_1=require("http-terminator"),Error_1=__importStar(require("../../../common/Error")),i18n_1=__importDefault(require("../../../i18n")),qs_1=__importDefault(require("qs"));class Server{constructor(e){this.url={},this.listeners=[],this.onErrorListener=[],this.timeout=!1,this.options=e,this.instance=http_1.default.createServer((t,r)=>{var i,s,o;if(this.timeout){const i={"en-US":{docTitle:"Login failed - Feishu Developer Tools",title:"login failed",text1:"Login to Feishu Developer Tools timed out",text2:"Please go back and try again"},"zh-CN":{docTitle:"登录失败 - 飞书开发者工具",title:"登录失败",text1:"飞书开发者工具登录超时",text2:"请返回并重试"}};r.writeHead(200);const s=this.getCurrentTemplateInfo(i,t,e.language),o=this.getTemplate("fail.html");return r.write(o(s)),this.onErrorListener.forEach(e=>e()),void this.close()}const n=null===(i=t.url)||void 0===i?void 0:i.split("?")[0],l=qs_1.default.parse(null!==(o=null===(s=t.url)||void 0===s?void 0:s.split("?")[1])&&void 0!==o?o:"");switch(n){case"/login":r.writeHead(302,{location:this.url.login});break;case"/succeed":case"/success":if(e.showSuccessPage){const i={"en-US":{docTitle:"Successful login - Feishu Developer Tools",title:"login success",text1:"Successfully Login to Feishu Developer Tools",text2:"Please go back and continue"},"zh-CN":{docTitle:"登录成功 - 飞书开发者工具",title:"登录成功",text1:"已成功登录飞书开发者工具",text2:"请返回并继续操作"}};r.writeHead(200);const s=this.getCurrentTemplateInfo(i,t,e.language),o=this.getTemplate("success.html");r.write(o(s))}else r.writeHead(204);this.listeners.forEach(e=>e(l)),this.close()}r.end()}),this.terminator=(0,http_terminator_1.createHttpTerminator)({server:this.instance})}getTemplate(e){const t=path_1.default.resolve(__dirname,"..","..","..","pages",e);return ejs_1.default.compile(fs_1.default.readFileSync(t,"utf-8"))}getCurrentTemplateInfo(e,t,r){let i;i=e.hasOwnProperty(r)?r:"default"===r&&t.headers["accept-language"]&&t.headers["accept-language"].includes("zh-CN")?"zh-CN":"en-US";return{language:r,i18n:e[i]}}start(){return this.instance.listen(),new Promise((e,t)=>{this.instance.on("listening",()=>{const r=this.instance.address();r?e("http://localhost:"+r.port):t(new Error_1.default({message:i18n_1.default.t("CORE_MODULE_ERROR_START_HTTP_FALIED"),code:Error_1.CoreErrorCode.CORE_MODULE_ERROR}))})})}close(){this.terminator.terminate()}set loginUrl(e){this.url.login=e+this.options.language}set isTimeout(e){this.timeout=e}onSuccess(e){this.listeners.push(e)}onError(e){this.onErrorListener.push(e)}}exports.default=Server;