"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.packCore=packCore,exports.default=pack;const tslib_1=require("tslib"),fs_1=tslib_1.__importDefault(require("fs")),path_1=tslib_1.__importDefault(require("path")),glob_1=tslib_1.__importDefault(require("glob")),rtrim_1=tslib_1.__importDefault(require("licia/rtrim")),fs_extra_1=tslib_1.__importDefault(require("fs-extra")),tools_1=require("../tools"),B_PROTOCOL=0,B_PROTOCOL_VERSION=1,B_FILEINFO_LEN=2,B_FILEDATA_LEN=3,B_PROTOCOL_END=4;function packCore(t){const e=[Buffer.alloc(1),Buffer.alloc(4),Buffer.alloc(4),Buffer.alloc(4),Buffer.alloc(1)];e[0].writeUIntLE(190,0,1),e[1].writeInt32BE(0,0),e[4].writeUIntLE(237,0,1);const r=t.length,l=[],a=[];t.forEach(({path:t,data:e})=>{l.push(Buffer.from(t)),a.push(e)});let o=18+12*r+Buffer.concat(l).length;const f=l.map((t,e)=>{const r=Buffer.alloc(4);r.writeInt32BE(t.length,0);const l=a[e].length,f=Buffer.alloc(4);f.writeInt32BE(o,0),o+=l;const n=Buffer.alloc(4);return n.writeInt32BE(l,0),Buffer.concat([r,t,f,n])}),n=Buffer.alloc(4);n.writeInt32BE(r,0),f.unshift(n);const c=Buffer.concat(f),i=Buffer.concat(a);e[2].writeInt32BE(c.length,0),e[3].writeInt32BE(i.length,0);const u=Buffer.concat(e);return Buffer.concat([u,c,i])}async function pack(t,e,r,l){const a=packCore(await readFiles(t,r,l));return fs_extra_1.default.ensureFileSync(e),fs_extra_1.default.writeFileSync(e,a),a}function readFiles(t,e,r){return t=(0,tools_1.normalizePath)(t),t=(0,rtrim_1.default)(t," /"),new Promise((l,a)=>{const o=[];(0,glob_1.default)(path_1.default.join(t,"**/*"),{nodir:!0,ignore:r,cwd:e},async(r,f)=>{if(r)return a(r);for(let r=0,l=f.length;r<l;r++){const l=f[r];o[r]={path:l.replace(t,""),data:fs_1.default.readFileSync(path_1.default.join(e,l))}}l(o)})})}