"use strict";const yauzl=require("yauzl"),stream=require("stream"),UncompressBaseStream=require("../base_write_stream"),utils=require("../utils"),YAUZL_CALLBACK=Symbol("ZipUncompressStream#yauzlCallback"),STRIP_NAME=Symbol("ZipUncompressStream#stripName"),DEFAULTS={lazyEntries:!0,decodeStrings:!1};class ZipUncompressStream extends UncompressBaseStream{constructor(e){super(e=e||{}),this._chunks=[],this._strip=Number(e.strip)||0,this[YAUZL_CALLBACK]=this[YAUZL_CALLBACK].bind(this);const t=utils.sourceType(e.source),r=this._yauzlOpts=Object.assign({},DEFAULTS,e.yauzl);"file"!==t?"buffer"!==t?"stream"!==t?this.on("pipe",e=>{e.unpipe(e),utils.streamToBuffer(e).then(e=>{this._chunks.push(e),e=Buffer.concat(this._chunks),yauzl.fromBuffer(e,r,this[YAUZL_CALLBACK])}).catch(e=>this.emit("error",e))}):utils.streamToBuffer(e.source).then(e=>yauzl.fromBuffer(e,r,this[YAUZL_CALLBACK])).catch(e=>this.emit("error",e)):yauzl.fromBuffer(e.source,r,this[YAUZL_CALLBACK]):yauzl.open(e.source,r,this[YAUZL_CALLBACK])}_write(e){this._chunks.push(e)}[YAUZL_CALLBACK](e,t){if(e)return this.emit("error",e);function r(){t.readEntry()}t.readEntry(),t.on("entry",e=>{const s=/\/$/.test(e.fileName)?"directory":"file",i={name:e.fileName=this[STRIP_NAME](e.fileName,s),type:s,yauzl:e};if("file"===s)t.openReadStream(e,(e,t)=>{if(e)return this.emit("error",e);this.emit("entry",i,t,r)});else{const e=new stream.Readable({read(){}});this.emit("entry",i,e,r),setImmediate(()=>e.emit("end"))}}).on("end",()=>this.emit("finish")).on("error",e=>this.emit("error",e))}[STRIP_NAME](e,t){return utils.stripFileName(this._strip,e,t)}}module.exports=ZipUncompressStream;