/*
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 { Base, markType, typeCast, Stream, Number_$type, IDisposable_$type } from "igniteui-angular-core";
import { IComparer$1_$type } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { BitConverter } from "igniteui-angular-core";
import { StringUtilities } from "./StringUtilities";
import { dateToFileTime, dateNow } from "igniteui-angular-core";
import { startsWith1, stringEquals1, stringCompare1 } from "igniteui-angular-core";
import { Buffer } from "igniteui-angular-core";
import { intDivide, truncate, logBase } from "igniteui-angular-core";
import { Encoding } from "igniteui-angular-core";
import { stringCreateFromCharArraySlice, stringEmpty } from "igniteui-angular-core";
import { Dictionary$2 } from "igniteui-angular-core";
import { BinaryReader } from "igniteui-angular-core";
import { SeekableBufferedStream } from "./SeekableBufferedStream";
import { Guid } from "igniteui-angular-core";
import { BinaryWriter } from "igniteui-angular-core";
/**
 * @hidden
 */
export class DirectoryEntry extends Base {
    constructor(a, b) {
        super();
        this.o = 0;
        this.s = null;
        this.q = 0;
        this.a = null;
        this.c = null;
        this.e = null;
        this.g = null;
        this.i = null;
        this.k = null;
        this.s = a;
        this.k = b;
    }
    static u(a, b) {
        if (a.length != b) {
        }
    }
    get p() {
        return this.o;
    }
    set p(a) {
        this.o = a;
    }
    get t() {
        return this.s;
    }
    get l() {
        return this.k;
    }
    get j() {
        return this.i;
    }
    set j(a) {
        this.i = a;
    }
    get r() {
        return this.q;
    }
    set r(a) {
        this.q = a;
    }
    get b() {
        if (this.a == null) {
            this.a = new Array(8);
        }
        return this.a;
    }
    set b(a) {
        DirectoryEntry.u(a, 8);
        this.a = a;
    }
    get d() {
        if (this.c == null) {
            this.c = new Array(8);
        }
        return this.c;
    }
    set d(a) {
        DirectoryEntry.u(a, 8);
        this.c = a;
    }
    get f() {
        if (this.e == null) {
            this.e = new Array(16);
        }
        return this.e;
    }
    set f(a) {
        DirectoryEntry.u(a, 16);
        this.e = a;
    }
    get h() {
        if (this.g == null) {
            this.g = new Array(4);
        }
        return this.g;
    }
    set h(a) {
        DirectoryEntry.u(a, 4);
        this.g = a;
    }
    static get n() {
        if (DirectoryEntry.m == null) {
            DirectoryEntry.m = new DirectoryEntry_NameComparer();
        }
        return DirectoryEntry.m;
    }
}
DirectoryEntry.$t = markType(DirectoryEntry, 'DirectoryEntry');
DirectoryEntry.m = null;
/**
 * @hidden
 */
export class Directory extends DirectoryEntry {
    constructor(a, b) {
        super(a, b);
        this.x = null;
        this.x = new List$1(DirectoryEntry.$, 0);
    }
    v(a, b) {
        let c = new DirectoryStream_DirectoryEntryNode();
        let d = null;
        let e;
        let f = a.indexOf('\\');
        if (f < 0) {
            for (let g = 0; g < this.y.count; g++) {
                let h = typeCast(File.$, this.y._inner[g]);
                if (h != null && h.t == a) {
                    return h;
                }
            }
            e = new File(a, c);
            d = e;
        }
        else {
            let i = a.substr(0, f);
            for (let j = 0; j < this.y.count; j++) {
                let k = typeCast(Directory.$, this.y._inner[j]);
                if (k != null && k.t == i) {
                    return k.v(a.substr(f + 1), b);
                }
            }
            let l = new Directory(i, c);
            e = l.v(a.substr(f + 1), b);
            d = l;
        }
        d.p = -2;
        d.b = BitConverter.f(dateToFileTime(dateNow()));
        d.d = d.b;
        c.c = -1;
        c.d = -1;
        c.e = -1;
        c.b = 0;
        c.a = d;
        d.j = this;
        let m;
        for (m = 0; m < this.y.count; m++) {
            if (DirectoryEntry.n.compare(d, this.y._inner[m]) < 0) {
                break;
            }
        }
        this.y.insert(m, d);
        b._aw.add(c);
        return e;
    }
    w(a, b) {
        for (let c = 0; c < this.x.count; c++) {
            let d = this.x._inner[c];
            let e = a + d.t;
            let f = typeCast(Directory.$, d);
            if (f != null) {
                if (startsWith1(b, e, StringUtilities.j)) {
                    return f.w(e + '\\', b);
                }
            }
            else if (stringEquals1(e, b, StringUtilities.j)) {
                return d;
            }
        }
        return null;
    }
    get y() {
        return this.x;
    }
}
Directory.$t = markType(Directory, 'Directory', DirectoryEntry.$);
/**
 * @hidden
 */
export class DirectoryEntry_NameComparer extends Base {
    compare(a, b) {
        if (a.t.length < b.t.length) {
            return -1;
        }
        if (a.t.length == b.t.length) {
            return stringCompare1(a.t, b.t, 5);
        }
        return 1;
    }
}
DirectoryEntry_NameComparer.$t = markType(DirectoryEntry_NameComparer, 'DirectoryEntry_NameComparer', Base.$, [IComparer$1_$type.specialize(DirectoryEntry.$)]);
/**
 * @hidden
 */
export class StructuredStorageStream extends Stream {
    constructor(a, ..._rest) {
        super();
        this._q = null;
        this._aj = null;
        this._y = 0;
        this._ag = 0;
        this._t = false;
        this._ab = 0;
        this._ac = 0;
        this._w = 0;
        this._x = 0;
        this._ad = 0;
        this._ai = 0;
        this._o = null;
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    let f = [c, d, e, false];
                    {
                        let h = f[0];
                        let i = f[1];
                        let j = f[2];
                        let k = f[3];
                        this._o = new Array(8);
                        this._q = h;
                        this._y = i;
                        this._ab = j;
                        this._t = k;
                        this._al();
                    }
                }
                break;
            case 1:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    let f = _rest[3];
                    this._o = new Array(8);
                    this._q = c;
                    this._y = d;
                    this._ab = e;
                    this._t = f;
                    this._al();
                }
                break;
        }
    }
    get_canRead() {
        return true;
    }
    get canRead() {
        return this.get_canRead();
    }
    get_canSeek() {
        return true;
    }
    get canSeek() {
        return this.get_canSeek();
    }
    get_canWrite() {
        return true;
    }
    get canWrite() {
        return this.get_canWrite();
    }
    disposeCore(a) {
        if (a) {
            if (this._q != null && this._q._g) {
                let b = this.length % this._ag;
                if (b != 0) {
                    this.position = this.length;
                    let c = (this._ag - b);
                    let d = new Array(c);
                    this._ah(d, 0, d.length);
                }
            }
        }
        super.disposeCore(a);
    }
    flush() {
    }
    get_length() {
        return this._ab;
    }
    get length() {
        return this.get_length();
    }
    get_position() {
        return this._ac;
    }
    set_position(a) {
        if (this._ac == a) {
            return;
        }
        if (a < 0) {
            return;
        }
        let b = (a - this._ac);
        let c = this._ad + b;
        this._ac = a;
        if (0 <= c && c < this._ag && this._ai == this._aj.position) {
            this._ad = c;
            this._ai += b;
            this._aj.position = this._ai;
        }
        else {
            let d = this._x;
            let e = this._w;
            let f = this._ad;
            this._ap();
            if (this._w >= 0) {
                this._ai = this._q._y(this._w, this._ad, this._t);
            }
            else {
                this._x = d;
                this._w = e;
                this._ad = f;
                this._ai = -1;
            }
        }
    }
    get position() {
        return this.get_position();
    }
    set position(a) {
        this.set_position(a);
    }
    read(a, b, c) {
        if (a == null) {
            return 0;
        }
        if (b < 0) {
            return 0;
        }
        if (c < 0) {
            return 0;
        }
        if (b + c > a.length) {
            return 0;
        }
        let d = Math.min(c, (this._ab - this._ac));
        if (d < 0) {
            return 0;
        }
        let e = 0;
        while (e < d) {
            let f = this._ae(a, b + e, d - e);
            if (f == 0) {
                break;
            }
            e += f;
        }
        return e;
    }
    seek(a, b) {
        switch (b) {
            case 0:
                this.position = a;
                break;
            case 1:
                this.position = this._ac + a;
                break;
            case 2:
                this.position = this._ab + a;
                break;
            default: break;
        }
        return this._ac;
    }
    setLength(a) {
        if (a == this._ab) {
            return;
        }
        if (a < this._ab) {
            return;
        }
        if (this._y < 0) {
            this._am(this._v(-1));
        }
        let b = ((intDivide((a - 1), this._ag)) + 1);
        let c = this._x;
        let d = this._w;
        if (d < 0) {
            c = 0;
            d = this._y;
        }
        while (true) {
            if (b <= c + 1) {
                break;
            }
            let e = 1;
            let f = ((() => { let g = this._aa(d, e); e = g.p1; return g.ret; })());
            if (f < 0) {
                break;
            }
            d = f;
            c++;
        }
        while (c < b - 1) {
            d = this._v(d);
            c++;
        }
        this._an(a);
    }
    write(a, b, c) {
        if (a == null) {
            return;
        }
        if (b < 0) {
            return;
        }
        if (c < 0) {
            return;
        }
        if (b + c > a.length) {
            return;
        }
        let d = 0;
        let e = this._ac + c;
        if (e > this._ab) {
            this.setLength(e);
        }
        while (d < c) {
            d += this._ah(a, b + d, c - d);
        }
    }
    _v(a) {
        return this._q._l(a, this._t, this._s);
    }
    _aa(a, b) {
        return {
            ret: ((() => { let c = this._q._o(a, b, this._t); b = c.p1; return c.ret; })()),
            p1: b
        };
    }
    _readBytes(a) {
        let b = new Array(a);
        let c = this.read(b, 0, b.length);
        if (c == a) {
            return b;
        }
        if (c == 0) {
            return new Array(0);
        }
        let d = new Array(c);
        Buffer.c(b, 0, d, 0, c);
        return d;
    }
    _ae(a, b, c) {
        this._ao();
        if (this._ad == this._ag) {
            let d = 1;
            this._w = ((() => { let e = this._aa(this._w, d); d = e.p1; return e.ret; })());
            this._x++;
            this._ad = 0;
            if (this._w >= 0) {
                this._ai = this._q._z(this._w, this._t);
            }
            else {
                this._x = -1;
                this._ai = -1;
                return 0;
            }
        }
        let e = Math.min(c, this._ag - this._ad);
        let f = this._aj.read(a, b, e);
        this._ac += f;
        this._ad += f;
        this._ai += f;
        return f;
    }
    _readInt16() {
        if (this.read(this._o, 0, 2) != 2) {
            return -1;
        }
        return BitConverter.p(this._o, 0);
    }
    _readInt32() {
        if (this.read(this._o, 0, 4) != 4) {
            return -1;
        }
        return BitConverter.q(this._o, 0);
    }
    _readUInt16() {
        if (this.read(this._o, 0, 2) != 2) {
            return 0;
        }
        return BitConverter.w(this._o, 0);
    }
    _al() {
        this._w = -1;
        this._x = -1;
        this._ad = -1;
        this._ai = -1;
        this._ag = this._q._p(this._t);
        this._aj = this._q._ad(this._t);
    }
    _am(a) {
        this._y = a;
    }
    _an(a) {
        this._ab = a;
    }
    _ao() {
        if (this._ai == this._aj.position) {
            return;
        }
        this._ap();
        this._ai = this._q._aa(this._w, this._ad, this._t);
    }
    _ap() {
        if (this._w < 0 || this._x < 0) {
            this._w = this._y;
            this._x = 0;
        }
        this._ad = (this._ac % this._ag);
        let a = (intDivide(this._ac, this._ag));
        let b = a - this._x;
        this._w = ((() => { let c = this._aa(this._w, b); b = c.p1; return c.ret; })());
        this._x = a;
        if (this._w < 0) {
            this._x = -1;
            this._w = -1;
            this._ad = -1;
            this._ai = -1;
            return;
        }
    }
    _writeInt16(a) {
        this.write(BitConverter.d(a), 0, 2);
    }
    _writeInt32(a) {
        this.write(BitConverter.e(a), 0, 4);
    }
    _ah(a, b, c) {
        this._ao();
        if (this._ad == this._ag) {
            let d = 1;
            let e = ((() => { let f = this._aa(this._w, d); d = f.p1; return f.ret; })());
            if (e < 0) {
                this._w = this._v(this._w);
            }
            else {
                this._w = e;
            }
            this._x++;
            this._ad = 0;
            this._ai = this._q._z(this._w, this._t);
        }
        let f = Math.min(c, this._ag - this._ad);
        this._aj.write(a, b, f);
        let g = (this._aj.position - this._ai);
        if (g > 0) {
            this._q._g = true;
        }
        this._ac += g;
        this._ad += g;
        this._ai += g;
        return g;
    }
    get _z() {
        return this._y;
    }
    get _storageManager() {
        return this._q;
    }
    get__s() {
        return 0;
    }
    get _s() {
        return this.get__s();
    }
}
StructuredStorageStream.$t = markType(StructuredStorageStream, 'StructuredStorageStream', Stream.$);
/**
 * @hidden
 */
export class DirectoryEntryStream extends StructuredStorageStream {
    constructor(a, ..._rest) {
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = [c, d, false];
                    {
                        let g = e[0];
                        let h = e[1];
                        let i = e[2];
                        super(1, g, h.p, h.r, i);
                        this._as = null;
                        this._at = false;
                        this._as = h;
                    }
                }
                break;
            case 1:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    super(1, c, d.p, d.r, e);
                    this._as = null;
                    this._at = false;
                    this._as = d;
                }
                break;
        }
    }
    disposeCore(a) {
        try {
            if (a && this._at) {
                this._as.d = BitConverter.f(dateToFileTime(dateNow()));
                this._at = false;
            }
        }
        finally {
            super.disposeCore(a);
        }
    }
    _am(a) {
        super._am(a);
        this._as.p = a;
    }
    _an(a) {
        super._an(a);
        this._as.r = a;
    }
    write(a, b, c) {
        super.write(a, b, c);
        if (c > 0) {
            this._at = true;
        }
    }
}
DirectoryEntryStream.$t = markType(DirectoryEntryStream, 'DirectoryEntryStream', StructuredStorageStream.$);
/**
 * @hidden
 */
export class DirectoryStream extends StructuredStorageStream {
    constructor(a, ..._rest) {
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    let c = _rest[0];
                    let d = [c, c._l(-1, false, 0)];
                    {
                        let f = d[0];
                        let g = d[1];
                        super(0, f, g, 0);
                        this._as = null;
                        this._av = null;
                        this._az = 0;
                        this._av = new List$1(DirectoryStream_DirectoryEntryNode.$, 0);
                        this._az = intDivide(this._ag, 128);
                    }
                    let e = new DirectoryStream_DirectoryEntryNode();
                    this._as = new Directory("Root Entry", e);
                    this._as.p = -2;
                    this._as.b = BitConverter.f(dateToFileTime(dateNow()));
                    this._as.d = this._as.b;
                    e.c = -1;
                    e.d = -1;
                    e.e = -1;
                    e.a = this._as;
                    e.b = 0;
                    this._av.add(e);
                    this._an(this._ag);
                }
                break;
            case 1:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    super(0, c, d, 0);
                    this._as = null;
                    this._av = null;
                    this._az = 0;
                    this._av = new List$1(DirectoryStream_DirectoryEntryNode.$, 0);
                    this._az = intDivide(this._ag, 128);
                }
                break;
        }
    }
    disposeCore(a) {
        try {
            if (a && this._storageManager._g) {
                this.position = 0;
                this._a0();
                let b;
                for (b = 0; b < this._av.count; b++) {
                    this._a5(this._av._inner[b]);
                }
                while (b % this._az != 0) {
                    this._a5(null);
                    b++;
                }
            }
        }
        finally {
            super.disposeCore(a);
        }
    }
    _addFile(a) {
        let b = this._as.v(a, this);
        if (b == null) {
            return null;
        }
        return b;
    }
    _a0() {
        for (let a = 0; a < this._av.count; a++) {
            let b = this._av._inner[a];
            let c = typeCast(Directory.$, b.a);
            if (c != null && c.y.count > 0) {
                let d = intDivide(c.y.count, 2);
                let e = c.y._inner[d];
                b.c = this._av.indexOf(e.l);
                this._a1(e, c.y, 0, c.y.count - 1);
            }
        }
    }
    _a1(a, b, c, d) {
        let e = b.indexOf(a);
        if (c < e) {
            let f = intDivide((c + e), 2);
            let g = b._inner[f];
            a.l.d = this._av.indexOf(g.l);
            this._a1(g, b, c, e - 1);
        }
        if (e < d) {
            let h = intDivide((d + e + 1), 2);
            let i = b._inner[h];
            a.l.e = this._av.indexOf(i.l);
            this._a1(i, b, e + 1, d);
        }
    }
    _readDirectoryEntryHeaders() {
        let a = this._z;
        while (a >= 0) {
            let b = 1;
            a = ((() => { let c = this._aa(a, b); b = c.p1; return c.ret; })());
            this._an((this.length + this._ag));
        }
        this.position = 0;
        while (this.position < this.length) {
            this._a3();
        }
        for (let c = 0; c < this._av.count; c++) {
            let d = this._av._inner[c];
            if (d.c < 0) {
                continue;
            }
            this._a4(d.a, d.c);
        }
    }
    _a3() {
        let a = new Array(32);
        let b = 0;
        while (b < 32) {
            a[b++] = String.fromCharCode(this._readUInt16());
        }
        let c = this._readInt16();
        let d = (intDivide(c, 2)) - 1;
        if (c == 0) {
            d = 0;
        }
        let e = stringCreateFromCharArraySlice(a, 0, d);
        let f = new DirectoryStream_DirectoryEntryNode();
        let g = this.readByte();
        f.b = this.readByte();
        f.d = this._readInt32();
        f.e = this._readInt32();
        f.c = this._readInt32();
        let h = this._readBytes(16);
        let i = this._readBytes(4);
        let j = this._readBytes(8);
        let k = this._readBytes(8);
        let l = this._readInt32();
        let m = this._readInt32();
        this._readBytes(4);
        if (g == 0) {
            return;
        }
        switch (g) {
            case 5:
                {
                    this._as = new Directory(e, f);
                    f.a = this._as;
                    break;
                }
            case 1:
                {
                    f.a = new Directory(e, f);
                    break;
                }
            case 2:
                {
                    f.a = new File(e, f);
                    break;
                }
            default:
                {
                    return;
                }
        }
        f.a.r = m;
        f.a.p = l;
        f.a.b = j;
        f.a.d = k;
        f.a.f = h;
        f.a.h = i;
        this._av.add(f);
    }
    _a4(a, b) {
        let c = this._av._inner[b];
        if (c.d >= 0) {
            this._a4(a, c.d);
        }
        c.a.j = a;
        a.y.add(c.a);
        if (c.e >= 0) {
            this._a4(a, c.e);
        }
    }
    _a5(a) {
        if (a == null) {
            let b = new Array((32 * 2) + 4);
            this.write(b, 0, b.length);
            this._writeInt32(-1);
            this._writeInt32(-1);
            this._writeInt32(-1);
            let c = new Array(48);
            this.write(c, 0, c.length);
        }
        else {
            let d = Encoding.unicode.getBytes1(a.a.t);
            let e = new Array((32 * 2) - d.length);
            this.write(d, 0, d.length);
            this.write(e, 0, e.length);
            this._writeInt16((d.length + 2));
            let f = 0;
            if (a.a == this._as) {
                f = 5;
            }
            else if (typeCast(File.$, a.a) !== null) {
                f = 2;
            }
            else if (typeCast(Directory.$, a.a) !== null) {
                f = 1;
            }
            else {
            }
            this.writeByte(f);
            this.writeByte(a.b);
            this._writeInt32(a.d);
            this._writeInt32(a.e);
            this._writeInt32(a.c);
            this.write(a.a.f, 0, 16);
            this.write(a.a.h, 0, 4);
            this.write(a.a.b, 0, 8);
            this.write(a.a.d, 0, 8);
            this._writeInt32(a.a.p);
            this._writeInt32(a.a.r);
            this._writeInt32(0);
        }
    }
    get _aw() {
        return this._av;
    }
    get _at() {
        return this._as;
    }
}
DirectoryStream.$t = markType(DirectoryStream, 'DirectoryStream', StructuredStorageStream.$);
/**
 * @hidden
 */
export class DirectoryStream_DirectoryEntryNode extends Base {
    constructor() {
        super();
        this.a = null;
        this.d = 0;
        this.e = 0;
        this.c = 0;
        this.b = 0;
    }
}
DirectoryStream_DirectoryEntryNode.$t = markType(DirectoryStream_DirectoryEntryNode, 'DirectoryStream_DirectoryEntryNode');
/**
 * @hidden
 */
export class File extends DirectoryEntry {
    constructor(a, b) {
        super(a, b);
    }
}
File.$t = markType(File, 'File', DirectoryEntry.$);
/**
 * @hidden
 */
export class MasterSATStream extends StructuredStorageStream {
    constructor(a, b, c, d) {
        super(0, a, b, c * a._p(false));
        this._a0 = 0;
        this._as = null;
        this._aw = null;
        this._ax = null;
        this._av = null;
        this._au = null;
        this._as = d;
        this._av = new Dictionary$2(Number_$type, Number_$type, 0);
        for (let e = 0; e < d.length; e++) {
            let f = d[e];
            if (0 <= f) {
                this._av.addItem(f, e);
            }
        }
        this._a0 = (intDivide(this._ag, 4)) - 1;
        this._ax = new List$1(Number_$type, 0);
        this._aw = new List$1(Number_$type, 0);
        this._au = new Dictionary$2(Number_$type, Number_$type, 0);
        let g = b;
        while (g >= 0) {
            this._storageManager._z(g, false);
            this._ax.add(g);
            for (let h = 0; h < this._ag - 4; h += 4) {
                let i = this._readInt32();
                this._aw.add(i);
                if (0 <= i) {
                    this._au.addItem(i, this._aw.count - 1);
                }
            }
            g = this._readInt32();
        }
    }
    _v(a) {
        let b = super._v(a);
        this._ax.add(b);
        return b;
    }
    disposeCore(a) {
        try {
            if (a && this._storageManager._g) {
                this.position = 0;
                let b = 0;
                for (let c = 0; c < this._ax.count; c++) {
                    for (let d = 0; d < this._a0; d++) {
                        this._writeInt32(this._aw._inner[b]);
                        b++;
                    }
                    if (c == this._ax.count - 1) {
                        this._writeInt32(-1);
                    }
                    else {
                        this._writeInt32(this._ax._inner[c + 1]);
                    }
                }
            }
        }
        finally {
            super.disposeCore(a);
        }
    }
    _aa(a, b) {
        if (b < 0) {
            let c = this._ax.count;
            while (b < 0) {
                if (this._ax._inner[0] == a) {
                    return {
                        ret: -2,
                        p1: b
                    };
                }
                let d = false;
                for (c = 1; c < this._ax.count; c++) {
                    if (this._ax._inner[c] == a) {
                        a = this._ax._inner[c - 1];
                        d = true;
                        break;
                    }
                }
                if (a < 0 || d == false) {
                    return {
                        ret: -2,
                        p1: b
                    };
                }
                b++;
            }
            if (b == 0) {
                return {
                    ret: a,
                    p1: b
                };
            }
            return {
                ret: -2,
                p1: b
            };
        }
        else {
            let e = this._ax.count;
            while (b > 0) {
                let f = false;
                for (e = 0; e < this._ax.count - 1; e++) {
                    if (this._ax._inner[e] == a) {
                        a = this._ax._inner[e + 1];
                        f = true;
                        break;
                    }
                }
                if (a < 0 || f == false) {
                    return {
                        ret: -2,
                        p1: b
                    };
                }
                b--;
            }
            if (b == 0) {
                return {
                    ret: a,
                    p1: b
                };
            }
            return {
                ret: -2,
                p1: b
            };
        }
    }
    get__s() {
        return 2;
    }
    _a1(a) {
        for (let b = 0; b < this._as.length; b++) {
            let c = this._as[b];
            if (c < 0) {
                this._as[b] = a;
                this._av.addItem(a, b);
                return;
            }
        }
        for (let d = 0; d < this._aw.count; d++) {
            let e = this._aw._inner[d];
            if (e < 0) {
                this._aw._inner[d] = a;
                this._au.addItem(a, d);
                return;
            }
        }
        this._aw.add(a);
        this._au.addItem(a, this._aw.count - 1);
        for (let f = 1; f < this._a0; f++) {
            this._aw.add(-1);
        }
        this.setLength(this.length + this._ag);
    }
    _ay(a) {
        let b;
        if (((() => { let c = this._av.tryGetValue(a, b); b = c.p1; return c.ret; })())) {
            if (b == this._as.length - 1) {
                if (this._aw.count == 0) {
                    return -1;
                }
                return this._aw._inner[0];
            }
            return this._as[b + 1];
        }
        let c;
        if (((() => { let d = this._au.tryGetValue(a, c); c = d.p1; return d.ret; })())) {
            if (c == this._aw.count - 1) {
                return -1;
            }
            return this._aw._inner[c + 1];
        }
        return -1;
    }
    _az(a) {
        let b;
        if (((() => { let c = this._av.tryGetValue(a, b); b = c.p1; return c.ret; })())) {
            if (b == 0) {
                return -1;
            }
            return this._as[b - 1];
        }
        let c;
        if (((() => { let d = this._au.tryGetValue(a, c); c = d.p1; return d.ret; })()) && 1 <= c) {
            if (c == 0) {
                return this._as[this._as.length - 1];
            }
            return this._aw._inner[c - 1];
        }
        return -1;
    }
    get _headerMasterSAT() {
        return this._as;
    }
}
MasterSATStream.$t = markType(MasterSATStream, 'MasterSATStream', StructuredStorageStream.$);
/**
 * @hidden
 */
export class SATStreamBase extends StructuredStorageStream {
    constructor(a, b, c) {
        super(0, a, b, c * a._p(false));
        this._aw = 0;
        this._as = null;
        this._at = null;
        this._aw = intDivide(a._p(false), 4);
    }
    disposeCore(a) {
        try {
            if (a && this._storageManager._g) {
                this.position = 0;
                for (let b = 0; b < this._as.count; b++) {
                    this._writeInt32(this._as._inner[b].b);
                }
            }
        }
        finally {
            super.disposeCore(a);
        }
    }
    _au(a, b) {
        let c = false;
        let d = Math.max(0, a);
        if (this._at.count > 0) {
            c = true;
            d = this._at._inner[0];
            this._at.removeAt(0);
        }
        if (c == false) {
            for (let e = 0; e < this._aw; e++) {
                this._at.add(this._as.count);
                this._as.add(SATStreamBase_SATEntry.a);
            }
            this.setLength(this.length + this._ag);
            while (d < this._as.count) {
                let f = this._as._inner[d].b;
                if (f == -1) {
                    this._at.remove(d);
                    c = true;
                    break;
                }
                d++;
            }
            if (c == false) {
                return this._au(a, b);
            }
        }
        if (a >= 0 && b == 0) {
            this._as._inner[a].b = d;
            this._as._inner[d].c = a;
        }
        switch (b) {
            case 2:
                this._as._inner[d].b = -4;
                break;
            case 0:
                this._as._inner[d].b = -2;
                break;
            case 1:
                this._as._inner[d].b = -3;
                break;
            default: break;
        }
        return d;
    }
    _getNextSectorIdInSAT(a, b) {
        while (b < 0) {
            a = this._as._inner[a].c;
            if (a < 0) {
                return {
                    ret: -2,
                    p1: b
                };
            }
            b++;
        }
        while (b > 0) {
            a = this._as._inner[a].b;
            if (a < 0) {
                return {
                    ret: -2,
                    p1: b
                };
            }
            b--;
        }
        return {
            ret: a,
            p1: b
        };
    }
    _ax(a, b) {
        let c = b * this._aw;
        this._as = new List$1(SATStreamBase_SATEntry.$, 2, c);
        this._at = new List$1(Number_$type, 0);
        if (a < 0) {
            return;
        }
        for (let d = 0; d < c; d++) {
            this._as.add(new SATStreamBase_SATEntry(-2, this._readInt32()));
        }
        for (let e = 0; e < this._as.count; e++) {
            let f = this._as._inner[e];
            if (f.b >= 0 && f.b < this._as.count) {
                this._as._inner[f.b].c = e;
            }
            else if (f.b == -1) {
                this._at.add(e);
            }
        }
    }
    _ay(a) {
        let b = a._z;
        while (b >= 0) {
            let c = 1;
            let d = ((() => { let e = a._aa(b, c); c = e.p1; return e.ret; })());
            this._as._inner[b].b = -1;
            let e = this._at.f(b);
            if (e < 0) {
                this._at.insert(~e, b);
            }
            b = d;
        }
    }
    _az(a) {
        this._as = a;
        this._at = new List$1(Number_$type, 0);
        for (let b = 0; b < this._as.count; b++) {
            if (this._as._inner[b].b == -1) {
                this._at.add(b);
            }
        }
    }
}
SATStreamBase.$t = markType(SATStreamBase, 'SATStreamBase', StructuredStorageStream.$);
/**
 * @hidden
 */
export class SATStream extends SATStreamBase {
    constructor(a, ..._rest) {
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    super(c, 0, 1);
                    this._a0 = null;
                    this._a0 = d;
                    let e = new List$1(SATStreamBase_SATEntry.$, 0);
                    e.add(new SATStreamBase_SATEntry(-2, -3));
                    for (let f = 1; f < this._aw; f++) {
                        e.add(SATStreamBase_SATEntry.a);
                    }
                    this._az(e);
                }
                break;
            case 1:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    let f = _rest[3];
                    super(c, d, e);
                    this._a0 = null;
                    this._a0 = f;
                    this._ax(d, e);
                }
                break;
        }
    }
    _v(a) {
        let b = super._v(a);
        this._a0._a1(b);
        return b;
    }
    disposeCore(a) {
        super.disposeCore(a);
        if (a) {
            this._a0.dispose();
        }
    }
    _aa(a, b) {
        while (b < 0) {
            a = this._a0._az(a);
            if (a < 0) {
                return {
                    ret: -1,
                    p1: b
                };
            }
            b++;
        }
        while (b > 0) {
            a = this._a0._ay(a);
            if (a < 0) {
                return {
                    ret: -1,
                    p1: b
                };
            }
            b--;
        }
        return {
            ret: a,
            p1: b
        };
    }
    get__s() {
        return 1;
    }
    get _masterSATStream() {
        return this._a0;
    }
}
SATStream.$t = markType(SATStream, 'SATStream', SATStreamBase.$);
/**
 * @hidden
 */
export class SATStreamBase_SATEntry extends Base {
    constructor(a, b) {
        super();
        this.c = 0;
        this.b = 0;
        this.c = a;
        this.b = b;
    }
    static get a() {
        return new SATStreamBase_SATEntry(-2, -1);
    }
}
SATStreamBase_SATEntry.$t = markType(SATStreamBase_SATEntry, 'SATStreamBase_SATEntry');
/**
 * @hidden
 */
export class ShortSATStream extends SATStreamBase {
    constructor(a, b, c) {
        super(a, b, c);
        this._ax(b, c);
    }
}
ShortSATStream.$t = markType(ShortSATStream, 'ShortSATStream', SATStreamBase.$);
/**
 * @hidden
 */
export class StructuredStorageManager extends Base {
    constructor(a, b) {
        super();
        this._k = new Guid();
        this._ag = 62;
        this._ah = 3;
        this._w = 512;
        this._x = 64;
        this._u = 4096;
        this._ae = null;
        this._b = null;
        this._d = null;
        this._e = null;
        this._c = null;
        this._h = false;
        this._f = false;
        this._ae = new SeekableBufferedStream(a);
        if (b) {
            let c = new BinaryReader(1, this._ae);
            let d = c.readUInt32();
            let e = c.readUInt32();
            if (d != 0xE011CFD0 || e != 0xE11AB1A1) {
                return;
            }
            this._k = new Guid(1, c.readBytes(16));
            this._ag = c.readUInt16();
            this._ah = c.readUInt16();
            let f = c.readUInt16();
            this._w = 1 << c.readUInt16();
            this._x = 1 << c.readUInt16();
            c.readBytes(10);
            let g = c.readInt32();
            let h = c.readInt32();
            c.readBytes(4);
            this._u = c.readInt32();
            let i = c.readInt32();
            let j = c.readInt32();
            let k = c.readInt32();
            let l = c.readInt32();
            let m = new Array(109);
            for (let n = 0; n < 109; n++) {
                m[n] = c.readInt32();
            }
            let o = new MasterSATStream(this, k, l, m);
            this._d = new SATStream(1, this, m[0], g, o);
            this._e = new ShortSATStream(this, i, j);
            this._c = new DirectoryStream(1, this, h);
            this._c._readDirectoryEntryHeaders();
            this._b = new DirectoryEntryStream(0, this, this._c._at);
        }
        else {
            let p = new Array(109);
            p[0] = 0;
            for (let q = 1; q < 109; q++) {
                p[q] = -1;
            }
            let r = new MasterSATStream(this, -2, 0, p);
            this._d = new SATStream(0, this, r);
            this._e = new ShortSATStream(this, -2, 0);
            this._c = new DirectoryStream(0, this);
            this._b = new DirectoryEntryStream(0, this, this._c._at);
        }
        this._h = true;
    }
    _ab(a) {
        let b = this._c._addFile(a);
        if (b == null) {
            return null;
        }
        this._f = true;
        return new UserFileStream(this, b);
    }
    _ac(a) {
        if (this._h == false) {
            return null;
        }
        let b = this._c._at.w(stringEmpty(), a);
        if (b == null) {
            return null;
        }
        return new UserFileStream(this, b);
    }
    static _j(a) {
        if (a.length < 8) {
            return false;
        }
        a.position = 0;
        try {
            let b = new BinaryReader(1, a);
            let c = b.readUInt32();
            let d = b.readUInt32();
            return c == 0xE011CFD0 && d == 0xE11AB1A1;
        }
        finally {
            a.position = 0;
        }
    }
    _dispose1(a) {
        if (this._h) {
            if (a && this._f) {
                this._c.dispose();
                this._b.dispose();
                this._e.dispose();
                this._d.dispose();
                this._ae.position = 0;
                let b = new BinaryWriter(1, this._ae);
                b.w(0xE011CFD0);
                b.w(0xE11AB1A1);
                b.h(this._k.toByteArray());
                b.v(this._ag);
                b.v(this._ah);
                b.v(65534);
                let c = truncate(Math.round(logBase(this._w, 2)));
                b.v(c);
                let d = truncate(Math.round(logBase(this._x, 2)));
                b.v(d);
                b.h(new Array(10));
                b.q(((intDivide((this._d.length - 1), this._w)) + 1));
                b.q(this._c._z);
                b.h(new Array(4));
                b.q(this._u);
                b.q(this._e._z);
                if (this._e.length == 0) {
                    b.q(0);
                }
                else {
                    b.q(((intDivide((this._e.length - 1), this._w)) + 1));
                }
                b.q(this._d._masterSATStream._z);
                if (this._d._masterSATStream.length == 0) {
                    b.q(0);
                }
                else {
                    b.q(((intDivide((this._d._masterSATStream.length - 1), this._w)) + 1));
                }
                let e = this._d._masterSATStream._headerMasterSAT;
                for (let f = 0; f < e.length; f++) {
                    b.q(e[f]);
                }
                this._ae.flush();
                this._ae.position = this._ae.length;
            }
            this._c = null;
            this._b = null;
            this._e = null;
            this._d = null;
            this._ae = null;
            this._f = false;
            this._h = false;
        }
    }
    _l(a, b, c) {
        if (b) {
            return this._e._au(a, c);
        }
        return this._d._au(a, c);
    }
    _y(a, b, c) {
        if (c) {
            return (a * this._x) + b;
        }
        return 512 + (a * this._w) + b;
    }
    _ad(a) {
        if (a) {
            return this._b;
        }
        return this._ae;
    }
    _o(a, b, c) {
        if (c) {
            return {
                ret: ((() => { let d = this._e._getNextSectorIdInSAT(a, b); b = d.p1; return d.ret; })()),
                p1: b
            };
        }
        return {
            ret: ((() => { let d = this._d._getNextSectorIdInSAT(a, b); b = d.p1; return d.ret; })()),
            p1: b
        };
    }
    _p(a) {
        if (a) {
            return this._x;
        }
        return this._w;
    }
    _i(a) {
        return a < this._u;
    }
    _z(a, b) {
        return this._aa(a, 0, b);
    }
    _aa(a, b, c) {
        let d = this._y(a, b, c);
        this._ad(c).position = d;
        return d;
    }
    _am(a) {
        if (a._isShortStream) {
            this._e._ay(a);
        }
        else {
            this._d._ay(a);
        }
    }
    get _g() {
        return this._f;
    }
    set _g(a) {
        this._f = a;
    }
    get _a() {
        return this._c._at;
    }
    dispose() {
        this._dispose1(true);
    }
}
StructuredStorageManager.$t = markType(StructuredStorageManager, 'StructuredStorageManager', Base.$, [IDisposable_$type]);
/**
 * @hidden
 */
export class UserFileStream extends DirectoryEntryStream {
    constructor(a, b) {
        super(1, a, b, a._i(b.r));
        this._au = null;
        this._au = b;
    }
    setLength(a) {
        if (this._isShortStream && this._storageManager._i(a) == false) {
            let b = this.position;
            let c = new Array(this.length);
            if (c.length > 0) {
                this.position = 0;
                this.read(c, 0, c.length);
                this._an(0);
            }
            this._storageManager._am(this);
            this._t = false;
            this._al();
            this._am(this._storageManager._l(-1, this._isShortStream, this._s));
            this._au.p = this._z;
            if (c.length > 0) {
                this.position = 0;
                this.write(c, 0, c.length);
                this.position = b;
            }
        }
        super.setLength(a);
    }
    get _isShortStream() {
        return this._t;
    }
}
UserFileStream.$t = markType(UserFileStream, 'UserFileStream', DirectoryEntryStream.$);
//# sourceMappingURL=DirectoryEntry_combined.js.map