"use strict";function isRegExp(e){return"[object RegExp]"===Object.prototype.toString.call(e)}var utf8=require("./utf8"),utils=require("./utils"),GenericWorker=require("./stream/GenericWorker"),StreamHelper=require("./stream/StreamHelper"),defaults=require("./defaults"),CompressedObject=require("./compressedObject"),ZipObject=require("./zipObject"),generate=require("./generate"),nodejsUtils=require("./nodejsUtils"),NodejsStreamInputAdapter=require("./nodejs/NodejsStreamInputAdapter"),fileAdd=function(e,r,t){var i,n=utils.getTypeOf(r);t=utils.extend(t||{},defaults),t.date=t.date||new Date,null!==t.compression&&(t.compression=t.compression.toUpperCase()),"string"==typeof t.unixPermissions&&(t.unixPermissions=parseInt(t.unixPermissions,8)),t.unixPermissions&&16384&t.unixPermissions&&(t.dir=!0),t.dosPermissions&&16&t.dosPermissions&&(t.dir=!0),t.dir&&(e=forceTrailingSlash(e)),t.createFolders&&(i=parentFolder(e))&&folderAdd.call(this,i,!0);var s="string"===n&&t.binary===!1&&t.base64===!1;t.binary=!s;var o=r instanceof CompressedObject&&0===r.uncompressedSize;(o||t.dir||!r||0===r.length)&&(t.base64=!1,t.binary=!0,r="",t.compression="STORE",n="string");var a=null;a=r instanceof CompressedObject||r instanceof GenericWorker?r:nodejsUtils.isNode&&nodejsUtils.isStream(r)?new NodejsStreamInputAdapter(e,r):utils.prepareContent(e,r,t.binary,t.optimizedBinaryString,t.base64);var l=new ZipObject(e,a,t);this.files[e]=l},parentFolder=function(e){"/"===e.slice(-1)&&(e=e.substring(0,e.length-1));var r=e.lastIndexOf("/");return r>0?e.substring(0,r):""},forceTrailingSlash=function(e){return"/"!==e.slice(-1)&&(e+="/"),e},folderAdd=function(e,r){return r="undefined"!=typeof r?r:defaults.createFolders,e=forceTrailingSlash(e),this.files[e]||fileAdd.call(this,e,null,{dir:!0,createFolders:r}),this.files[e]},out={load:function(){throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide.")},forEach:function(e){var r,t,i;for(r in this.files)this.files.hasOwnProperty(r)&&(i=this.files[r],t=r.slice(this.root.length,r.length),t&&r.slice(0,this.root.length)===this.root&&e(t,i))},filter:function(e){var r=[];return this.forEach(function(t,i){e(t,i)&&r.push(i)}),r},file:function(e,r,t){if(1===arguments.length){if(isRegExp(e)){var i=e;return this.filter(function(e,r){return!r.dir&&i.test(e)})}var n=this.files[this.root+e];return n&&!n.dir?n:null}return e=this.root+e,fileAdd.call(this,e,r,t),this},folder:function(e){if(!e)return this;if(isRegExp(e))return this.filter(function(r,t){return t.dir&&e.test(r)});var r=this.root+e,t=folderAdd.call(this,r),i=this.clone();return i.root=t.name,i},remove:function(e){e=this.root+e;var r=this.files[e];if(r||("/"!==e.slice(-1)&&(e+="/"),r=this.files[e]),r&&!r.dir)delete this.files[e];else for(var t=this.filter(function(r,t){return t.name.slice(0,e.length)===e}),i=0;i<t.length;i++)delete this.files[t[i].name];return this},generate:function(e){throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide.")},generateInternalStream:function(e){var r,t={};try{if(t=utils.extend(e||{},{streamFiles:!1,compression:"STORE",compressionOptions:null,type:"",platform:"DOS",comment:null,mimeType:"application/zip",encodeFileName:utf8.utf8encode}),t.type=t.type.toLowerCase(),t.compression=t.compression.toUpperCase(),"binarystring"===t.type&&(t.type="string"),!t.type)throw new Error("No output type specified.");utils.checkSupport(t.type),"darwin"!==e.platform&&"freebsd"!==e.platform&&"linux"!==e.platform&&"sunos"!==e.platform||(e.platform="UNIX"),"win32"===e.platform&&(e.platform="DOS");var i=t.comment||this.comment||"";r=generate.generateWorker(this,t,i)}catch(n){r=new GenericWorker("error"),r.error(n)}return new StreamHelper(r,t.type||"string",t.mimeType)},generateAsync:function(e,r){return this.generateInternalStream(e).accumulate(r)},generateNodeStream:function(e,r){return e=e||{},e.type||(e.type="nodebuffer"),this.generateInternalStream(e).toNodejsStream(r)}};module.exports=out;