"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,n,o)}:function(e,t,r,n){void 0===n&&(n=r),e[n]=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},__awaiter=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function _(e){try{u(n.next(e))}catch(e){i(e)}}function a(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(_,a)}u((n=n.apply(e,t||[])).next())}))},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.unpack=void 0;const fs_extra_1=__importDefault(require("fs-extra")),path_1=__importDefault(require("path")),rimraf_1=__importDefault(require("rimraf")),unzipper_1=__importDefault(require("unzipper")),node_1=__importDefault(require("@lark-opdev/i18n/libs/node")),Error_1=__importStar(require("../../../common/Error")),TAG_LEN=4;let DIST="";function writeFile(e,t){return __awaiter(this,void 0,void 0,(function*(){try{return yield fs_extra_1.default.ensureFile(e),fs_extra_1.default.writeFile(e,t)}catch(e){throw e}}))}function writeFiles(e){return __awaiter(this,void 0,void 0,(function*(){try{const t=Object.keys(e);for(let r=0;r<t.length;r++){const n=t[r];yield writeFile(n,e[n])}}catch(e){throw e}}))}function readBytes(e,t,r){const n=t+r;if(n>e.length)throw new Error(node_1.default.t("UNPACK_FAIL_EXCEED_LIMIT_ERROR"));return e.slice(t,n)}function decodeHeader(e,t,r){if(!e)throw new Error(node_1.default.t("UNPACK_FAIL_NO_DATA_DECODE_ERROR"));const n=readBytes(e,0,r).toString();t+=r;const o=e.readUInt32LE(t);t+=4;const i=e.readUInt32LE(t);return{tag:n,version:o,ext:readBytes(e,t+=4,i).toString(),offset:t+=i}}function decodeBody(e,t){if(!e)throw new Error(node_1.default.t("UNPACK_FAIL_NO_DATA_DECODE_ERROR"));const r=e.readUInt32LE(t);t+=4;const n={};for(let o=0;o<r;o++){const r=e.readUInt32LE(t),o=readBytes(e,t+=4,r).toString();t+=r;const i=e.readUInt32LE(t);t+=4;const _=e.readUInt32LE(t);t+=4;const a=readBytes(e,i,_);n[path_1.default.join(DIST,o)]=a}return n}function unZip(e,t){return new Promise((r,n)=>{fs_extra_1.default.createReadStream(e).pipe(unzipper_1.default.Extract({path:t})).on("error",n).on("finish",r)})}function unpack(e,t,r="pkg"){return __awaiter(this,void 0,void 0,(function*(){if(!e)throw new Error_1.default({code:Error_1.CoreErrorCode.UNPACK_FAIL,message:node_1.default.t("UNPACK_FAIL_ERROR",{errorStr:node_1.default.t("UNPACK_FAIL_INVALID_PKG_PATH_ERROR")}),advise:node_1.default.t("UNPACK_FAIL_INVALID_PKG_PATH_ADVISE")});t||(t=path_1.default.join(__dirname,"../../public/__unpack__"));let n=0;try{if(rimraf_1.default.sync(t),DIST=t,"pkg"===r){const t=fs_extra_1.default.readFileSync(e);n=decodeHeader(t,n,4).offset;const r=decodeBody(t,n);yield writeFiles(r)}else"zip"===r&&(yield unZip(e,t))}catch(e){throw new Error_1.default({code:Error_1.CoreErrorCode.UNPACK_FAIL,message:node_1.default.t("UNPACK_FAIL_ERROR",{errorStr:e.message}),advise:node_1.default.t("UNPACK_FAIL_ERROR_ADVISE")})}}))}exports.unpack=unpack;