/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import * as tslib_1 from "tslib";
import { ExternalEventWorkItem$1 } from "./ExternalEventWorkItem$1";
import { ExternalEventWorkItem } from "./ExternalEventWorkItem";
import { BaseError, Base, Stream, typeCast, fromEnum, markType } from "igniteui-angular-core";
import { IZipFile_$type } from "./IZipFile";
import { List$1 } from "igniteui-angular-core";
import { IZipEntry_$type } from "./IZipEntry";
import { ZipEntry } from "./ZipEntry";
import { MemoryStream } from "igniteui-angular-core";
import { b64toUint8Array } from "igniteui-angular-core";
import * as JSZip from "jszip/dist/jszip";
/**
 * @hidden
 */
var ZipFile = /** @class */ (function (_super) {
    tslib_1.__extends(ZipFile, _super);
    function ZipFile() {
        var _this = _super.call(this) || this;
        _this.a = new List$1(IZipEntry_$type, 0);
        _this.b = null;
        _this.b = typeof JSZip.default === "function" ? new JSZip.default() : new JSZip();
        return _this;
    }
    ZipFile.prototype.createFile = function (fileName, directoryName, stream) {
        arguments[2] = (typeof arguments[2] === 'string' ? b64toUint8Array(arguments[2]) : arguments[2]);
        arguments[2] = (typeCast(Stream.$, arguments[2]) ? arguments[2] : new MemoryStream(2, arguments[2]));
        return this._createFile$i.apply(this, arguments);
    };
    ZipFile.prototype._createFile$i = function (a, b, c) {
        return ZipEntry.a(a, b, c);
    };
    ZipFile.prototype.load = function (stream) {
        arguments[0] = (typeof arguments[0] === 'string' ? b64toUint8Array(arguments[0]) : arguments[0]);
        arguments[0] = (typeCast(Stream.$, arguments[0]) ? arguments[0] : new MemoryStream(2, arguments[0]));
        return this._load$i.apply(this, arguments);
    };
    ZipFile.prototype._load$i = function (a) {
        var _this = this;
        var b = null;
        var c = typeCast(MemoryStream.$, a);
        if (c != null) {
            b = c.getBuffer();
            if (b.length != a.length) {
                b = null;
            }
        }
        if (b == null) {
            b = new Array(a.length);
            a.read(b, 0, b.length);
        }
        var d = new ExternalEventWorkItem$1(IZipFile_$type);
        var e = function (f) {
            var g = (Base.getArrayOfValues(_this.b.files));
            for (var h = 0; h < g.length; h++) {
                _this.a.add(new ZipEntry(0, g[h]));
            }
            d.ag(_this);
        };
        var f = function (g) { return d.ag(null, new BaseError(1, g != null ? g.toString() : "Load error")); };
        this.b.loadAsync(b).then(e, f);
        return d;
    };
    Object.defineProperty(ZipFile.prototype, "entries", {
        get: function () {
            return this.a;
        },
        enumerable: true,
        configurable: true
    });
    ZipFile.prototype.save = function (stream) {
        arguments[0] = (typeof arguments[0] === 'string' ? b64toUint8Array(arguments[0]) : arguments[0]);
        arguments[0] = (typeCast(Stream.$, arguments[0]) ? arguments[0] : new MemoryStream(2, arguments[0]));
        return this._save$i.apply(this, arguments);
    };
    ZipFile.prototype._save$i = function (a) {
        var e_1, _a;
        try {
            for (var _b = tslib_1.__values(fromEnum(this.a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                var c = b;
                var d = c.name;
                if (d.charAt(0) == '/') {
                    d = d.substr(1);
                }
                if (c.isDirectory) {
                    this.b.folder(d);
                }
                else {
                    var e = new Array(c.b.length);
                    c.b.position = 0;
                    c.b.read(e, 0, e.length);
                    this.b.file(d, e);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        var f = (JSZip.support.uint8array) ? "uint8array" : "array";
        var g = new ExternalEventWorkItem();
        var h = function (i) {
            var j = i;
            a.write(j, 0, j.length);
            g.af();
        };
        var i = function (j) { return g.af(new BaseError(1, j != null ? j.toString() : "Load error")); };
        this.b.generateAsync({ type: f, compression: 'DEFLATE' }).then(h, i);
        return g;
    };
    ZipFile.$t = markType(ZipFile, 'ZipFile', Base.$, [IZipFile_$type]);
    return ZipFile;
}(Base));
export { ZipFile };
//# sourceMappingURL=ZipFile.js.map