/*
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 { Base, Stream, typeCast, String_$type, EnumUtil, markType, callStaticConstructors, runOn, fromEnum, TypeRegistrar } from "igniteui-angular-core";
import { IPackagePart_$type } from "./IPackagePart";
import { IPackagePartEx_$type } from "./IPackagePartEx";
import { IPackage_$type } from "./IPackage";
import { Uri } from "igniteui-angular-core";
import { IZipEntry_$type } from "./IZipEntry";
import { IPackageRelationship_$type } from "./IPackageRelationship";
import { MemoryStream } from "igniteui-angular-core";
import { ArgumentException } from "igniteui-angular-core";
import { IOPath } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { WorkItemExtensions } from "./WorkItemExtensions";
import { RelationshipTargetMode_$type } from "./RelationshipTargetMode";
import { PackageRelationship } from "./PackageRelationship";
import { InvalidTargetPackageRelationship } from "./InvalidTargetPackageRelationship";
import { Dictionary$2 } from "igniteui-angular-core";
import { WorkItem } from "./WorkItem";
import { PackagePartExposedStream } from "./PackagePartExposedStream";
import { DocCoreExtensions } from "./DocCoreExtensions";
import { Enumerable } from "igniteui-angular-core";
import { Async } from "./Async";
import { XmlTextReader } from "igniteui-angular-core";
import { stringReplace, stringIsNullOrEmpty } from "igniteui-angular-core";
import { StringComparer } from "igniteui-angular-core";
import { ZipFile } from "./ZipFile";
import { RelationshipsWriter } from "./RelationshipsWriter";
import { ZipEntry } from "./ZipEntry";
import { XmlTextWriter } from "igniteui-angular-core";
import { Encoding } from "igniteui-angular-core";
/**
 * @hidden
 */
var PackagePart = /** @class */ (function (_super) {
    tslib_1.__extends(PackagePart, _super);
    function PackagePart(a, b, c) {
        var _this = _super.call(this) || this;
        _this.f = null;
        _this.d = null;
        _this._$package = null;
        _this._uri = null;
        _this._contentType = null;
        _this.b = null;
        _this.f = new MemoryStream(0);
        _this.$package = a;
        _this.uri = b;
        _this.contentType = c;
        var d = typeCast(Package.$, _this.$package);
        if (d == null) {
            throw new ArgumentException(2, "The expected type for package parameter is type of Package.", "package");
        }
        if (d._q == 2) {
            var e = IOPath.getFileName(b.toString());
            var f = IOPath.getDirectoryName(b.toString());
            f = stringReplace(f, '\\', '/');
            _this.b = d._c._createFile$i(e, f, new MemoryStream(0));
            d._c.entries.add(_this.b);
        }
        return _this;
    }
    Object.defineProperty(PackagePart.prototype, "$package", {
        get: function () {
            return this._$package;
        },
        set: function (a) {
            this._$package = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PackagePart.prototype, "uri", {
        get: function () {
            return this._uri;
        },
        set: function (a) {
            this._uri = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PackagePart.prototype, "contentType", {
        get: function () {
            return this._contentType;
        },
        set: function (a) {
            this._contentType = a;
        },
        enumerable: true,
        configurable: true
    });
    PackagePart.prototype.getStream = function (a, b) {
        return WorkItemExtensions.l(Stream.$, Stream.$, this.b.openReader(), function (c) { return WorkItem.g(Stream.$, new PackagePartExposedStream(WorkItemExtensions.m(Stream.$, c))); });
    };
    PackagePart.prototype.createRelationship = function (a, b, c, d) {
        var e = new PackageRelationship(1, this.uri, a, b, c, d);
        return this.a(e);
    };
    PackagePart.prototype.createExternalRelationship = function (a, b, c) {
        var d;
        if (((function () { var e = Uri.tryCreate(a, 0, d); d = e.p2; return e.ret; })())) {
            return this.createRelationship(d, 1, b, c);
        }
        var e = new InvalidTargetPackageRelationship(this.uri, a, 1, b, c);
        return this.a(e);
    };
    PackagePart.prototype.a = function (a) {
        if (this.d == null) {
            this.d = new Dictionary$2(String_$type, IPackageRelationship_$type, 1, 1);
        }
        if (this.d.count == 0) {
            var b = IOPath.getFileName(this.uri.toString()) + ".rels";
            var c = IOPath.getDirectoryName(this.uri.toString()) + "/_rels";
            if (c.charAt(0) == '\\') {
                c = c.substr(1);
            }
            var d = this.$package;
            var e = d._c._createFile$i(b, c, this.f);
            d._c.entries.add(e);
        }
        this.d.addItem(a.id, a);
        return a;
    };
    PackagePart.prototype.getRelationships = function () {
        this.j();
        return this.d.values;
    };
    PackagePart.prototype.getRelationship = function (a) {
        this.j();
        var b = null;
        var c = this.d.tryGetValue(a, b);
        b = c.p1;
        return b;
    };
    Object.defineProperty(PackagePart.prototype, "e", {
        get: function () {
            return this.getRelationships();
        },
        enumerable: true,
        configurable: true
    });
    PackagePart.prototype.c = function () {
        var _this = this;
        this.d = new Dictionary$2(String_$type, IPackageRelationship_$type, 0);
        var a = stringReplace(IOPath.getDirectoryName(DocCoreExtensions.fixDirectoryPath(this.uri.toString())), "\\", "/");
        var b = IOPath.getFileName(this.uri.toString());
        var c = this.$package;
        var d = c._c;
        var e = a + "/_rels/" + b + ".rels";
        var f = Enumerable.u(IZipEntry_$type, d.entries, function (g) { return g.name == e; });
        if (Enumerable.aj(IZipEntry_$type, f) != 1) {
            return null;
        }
        c._b = Enumerable.ad(IZipEntry_$type, f);
        return Async.o(Stream.$, c._b.openReader(), function (g) {
            _this.f = g;
            {
                var h = new XmlTextReader(1, _this.f);
                try {
                    while (h.t()) {
                        if (h.l() && h.ai == "Relationship") {
                            if (h.e) {
                                var i = h.ab("TargetMode");
                                var j = h.ab("Target");
                                var k = h.ab("Id");
                                var l = h.ab("Type");
                                var m = 0;
                                if (stringIsNullOrEmpty(i) == false) {
                                    m = EnumUtil.getEnumValue(RelationshipTargetMode_$type, EnumUtil.parse(RelationshipTargetMode_$type, i, true));
                                }
                                var n = new PackageRelationship(1, _this.uri, new Uri(1, j, 0), m, l, k);
                                _this.d.addItem(k, n);
                            }
                        }
                    }
                }
                finally {
                    if (h != null) {
                        h.dispose();
                    }
                }
            }
            return null;
        });
    };
    PackagePart.prototype.j = function () {
        if (this.d == null) {
            this.d = new Dictionary$2(String_$type, IPackageRelationship_$type, 0);
        }
    };
    PackagePart.$t = markType(PackagePart, 'PackagePart', Base.$, [IPackagePart_$type, IPackagePartEx_$type]);
    return PackagePart;
}(Base));
export { PackagePart };
/**
 * @hidden
 */
var Package = /** @class */ (function (_super) {
    tslib_1.__extends(Package, _super);
    function Package() {
        var _this = _super.call(this) || this;
        _this._i = false;
        _this._j = false;
        _this._s = null;
        _this._t = null;
        _this._r = null;
        _this._m = new Dictionary$2(String_$type, PackagePart.$, 0);
        _this._p = null;
        _this._k = new Dictionary$2(String_$type, String_$type, 2, StringComparer.g);
        _this._l = new Dictionary$2(String_$type, String_$type, 2, StringComparer.g);
        _this._q = 0;
        _this._c = null;
        _this._b = null;
        _this._a = null;
        _this._h = null;
        return _this;
    }
    Package.staticInit = function () {
        TypeRegistrar.register("Package", Package.$);
    };
    Object.defineProperty(Package.prototype, "_o", {
        get: function () {
            return this._p;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Package.prototype, "_n", {
        get: function () {
            return this._m;
        },
        enumerable: true,
        configurable: true
    });
    Package._d = function (a, b) {
        return Package._createUsingFile(a, b, new ZipFile());
    };
    Package._createUsingFile = function (a, b, c) {
        var d = new Package();
        d._q = b;
        d._c = c;
        if (b == 3) {
            a.position = 0;
            return WorkItemExtensions.g(IPackage_$type, WorkItemExtensions.c(WorkItemExtensions.c(d._c._load$i(a), runOn(d, d._f)), runOn(d, d._g)), function () { return d; });
        }
        else {
            d._s = a;
            return WorkItem.g(IPackage_$type, d);
        }
    };
    Package.prototype.createPart = function (a, b) {
        if (!this._i) {
            this._ah();
        }
        var c = new PackagePart(this, a, b);
        this._l.addItem(a.toString(), b);
        if (this.partExists(a)) {
            this._m.item(a.originalString, c);
        }
        else {
            this._m.addItem(a.originalString, c);
        }
        return c;
    };
    Package.prototype.createRelationship = function (a, b, c, d) {
        if (!this._j) {
            this._ai();
        }
        var e = new PackageRelationship(0, a, b, c, d);
        this._p.add(e);
        return e;
    };
    Package.prototype.getRelationships = function () {
        if (this._p == null) {
            this._ai();
        }
        return this._p;
    };
    Package.prototype.getPart = function (a) {
        var b = null;
        if (this.partExists(a)) {
            b = this._m.item(a.originalString);
        }
        return b;
    };
    Package.prototype.getParts = function () {
        return this._m.values;
    };
    Package.prototype.getRelationship = function (a) {
        if (this._p == null) {
            this._ai();
        }
        return Enumerable.af(IPackageRelationship_$type, Enumerable.u(IPackageRelationship_$type, this._p, function (b) { return b.id == a; }));
    };
    Package.prototype._aa = function (a) {
        if (this._l.containsKey(a)) {
            return this._l.item(a);
        }
        var b = IOPath.getExtension(a).substr(1);
        if (this._k.containsKey(b)) {
            return this._k.item(b);
        }
        return null;
    };
    Package.prototype._f = function () {
        var _this = this;
        var a = Enumerable.ad(IZipEntry_$type, Enumerable.u(IZipEntry_$type, this._c.entries, function (b) { return b.name == "[Content_Types].xml"; }));
        return WorkItemExtensions.c(Async.o(Stream.$, a.openReader(), function (b) {
            {
                var c = new XmlTextReader(1, b);
                try {
                    while (c.t()) {
                        if (c.l() && c.ai == "Types") {
                            while (c.t()) {
                                if (c.l()) {
                                    if (c.ai == "Override") {
                                        var d = c.ab("PartName");
                                        var e = c.ab("ContentType");
                                        if (!_this._l.containsKey(d)) {
                                            _this._l.addItem(d, e);
                                        }
                                    }
                                    else if (c.ai == "Default") {
                                        var f = c.ab("Extension");
                                        var g = c.ab("ContentType");
                                        if (!_this._k.containsKey(f)) {
                                            _this._k.addItem(f, g);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                finally {
                    if (c != null) {
                        c.dispose();
                    }
                }
            }
            return null;
        }), function () { return Async.d(IZipEntry_$type, Enumerable.u(IZipEntry_$type, _this._c.entries, function (b) { return b.name != "[Content_Types].xml" && !b.isDirectory; }), function (b) {
            var c = "/" + b.name;
            var d = _this._aa(c);
            var e = new PackagePart(_this, new Uri(1, c, 2), d);
            e.b = b;
            _this._m.addItem(e.uri.originalString, e);
            return e.c();
        }); });
    };
    Package.prototype._g = function () {
        var _this = this;
        this._p = new List$1(IPackageRelationship_$type, 0);
        this._b = Enumerable.ad(IZipEntry_$type, Enumerable.u(IZipEntry_$type, this._c.entries, function (a) { return a.name == "_rels/.rels"; }));
        return Async.o(Stream.$, this._b.openReader(), function (a) {
            _this._t = a;
            {
                var b = new XmlTextReader(1, a);
                try {
                    while (b.t()) {
                        if (b.l() && b.ai == "Relationship") {
                            if (b.e) {
                                var c = b.ab("TargetMode");
                                var d = b.ab("Id");
                                var e = b.ab("Type");
                                var f = b.ab("Target");
                                var g = 0;
                                if (stringIsNullOrEmpty(c) == false) {
                                    g = EnumUtil.getEnumValue(RelationshipTargetMode_$type, EnumUtil.parse(RelationshipTargetMode_$type, c, true));
                                }
                                var h = new PackageRelationship(1, new Uri(1, "/", 2), new Uri(1, f, 0), g, e, d);
                                _this._p.add(h);
                            }
                        }
                    }
                }
                finally {
                    if (b != null) {
                        b.dispose();
                    }
                }
            }
            return null;
        });
    };
    Package.prototype.partExists = function (a) {
        return this._m.containsKey(a.originalString);
    };
    Package.prototype.dispose = function () {
        var e_1, _e;
        var _this = this;
        if (this._q == 2) {
            if (this._t != null) {
                RelationshipsWriter.j(this._o, this._t);
            }
            try {
                for (var _u = tslib_1.__values(fromEnum(this._m.values)), _w = _u.next(); !_w.done; _w = _u.next()) {
                    var a = _w.value;
                    if (a.f != null) {
                        RelationshipsWriter.j(a.e, a.f);
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_w && !_w.done && (_e = _u.return)) _e.call(_u);
                }
                finally { if (e_1) throw e_1.error; }
            }
            if (this._r != null) {
                ContentTypesWriter.j(this, this._r);
            }
            if (this._s != null) {
                this._h = WorkItem.d(function () { return _this._c._save$i(_this._s); });
            }
        }
    };
    Package.prototype._ah = function () {
        this._r = new MemoryStream(0);
        this._a = ZipEntry.a("[Content_Types].xml", "/", this._r);
        this._c.entries.add(this._a);
        this._i = true;
    };
    Package.prototype._ai = function () {
        this._t = new MemoryStream(0);
        this._b = ZipEntry.a(".rels", "/_rels", this._t);
        this._c.entries.add(this._b);
        if (this._p == null) {
            this._p = new List$1(IPackageRelationship_$type, 0);
        }
        this._j = true;
    };
    Package.$t = markType(Package, 'Package', Base.$, [IPackage_$type]);
    Package._v = "http://schemas.openxmlformats.org/package/2006/content-types";
    return Package;
}(Base));
export { Package };
/**
 * @hidden
 */
var ContentTypesWriter = /** @class */ (function (_super) {
    tslib_1.__extends(ContentTypesWriter, _super);
    function ContentTypesWriter() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ContentTypesWriter.k = function (a, b, c) {
        a.an("Default", ContentTypesWriter.b);
        a.h("Extension", c);
        a.h("ContentType", b);
        a.v();
    };
    ContentTypesWriter.l = function (a, b, c) {
        a.an("Override", ContentTypesWriter.b);
        a.h("PartName", c);
        a.h("ContentType", b);
        a.v();
    };
    ContentTypesWriter.j = function (a, b) {
        var e_2, _e;
        {
            var c = new XmlTextWriter(0, b, Encoding.uTF8);
            try {
                c.ad("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"");
                c.an("Types", ContentTypesWriter.b);
                ContentTypesWriter.k(c, ContentTypesWriter.d, "rels");
                ContentTypesWriter.k(c, "application/xml", "xml");
                try {
                    for (var _u = tslib_1.__values(fromEnum(a._l)), _w = _u.next(); !_w.done; _w = _u.next()) {
                        var d = _w.value;
                        ContentTypesWriter.l(c, d.value, d.key);
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_w && !_w.done && (_e = _u.return)) _e.call(_u);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
                c.v();
            }
            finally {
                if (c != null) {
                    c.dispose();
                }
            }
        }
    };
    ContentTypesWriter.$t = markType(ContentTypesWriter, 'ContentTypesWriter');
    ContentTypesWriter.d = "application/vnd.openxmlformats-package.relationships+xml";
    ContentTypesWriter.b = "http://schemas.openxmlformats.org/package/2006/content-types";
    return ContentTypesWriter;
}(Base));
export { ContentTypesWriter };
(function (v) { callStaticConstructors(); })(true);
//# sourceMappingURL=PackagePart_combined.js.map