/*
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 { DependencyObjectNotifier } from "./DependencyObjectNotifier";
import { IList$1_$type, BaseError, delegateCombine, Point_$type, fromEnum, markType, markDep } from "./type";
import { ShapefileRecord } from "./ShapefileRecord";
import { INotifyCollectionChanged_$type } from "./INotifyCollectionChanged";
import { Rect } from "./Rect";
import { DependencyProperty } from "./DependencyProperty";
import { ObservableCollection$1 } from "./ObservableCollection$1";
import { List$1 } from "./List$1";
import { AsyncCompletedEventArgs } from "./AsyncCompletedEventArgs";
import { BinaryReader } from "./BinaryReader";
import { BinaryFileDownloader } from "./BinaryFileDownloader";
import { ShapeFilterRecordEventArgs } from "./ShapeFilterRecordEventArgs";
import { ShapeFileUtil } from "./ShapeFileUtil";
import { PropertyMetadata } from "./PropertyMetadata";
import { isNaN_, isInfinity } from "./number";
/**
 * @hidden
 */
var ShapefileConverter = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(ShapefileConverter, _super);
    function ShapefileConverter() {
        var _this = _super.call(this) || this;
        _this._name = null;
        _this.a8 = null;
        _this.a7 = Rect.empty;
        _this._shapeHeader = null;
        _this.q = false;
        _this.importPending = null;
        _this.importCompleted = null;
        _this.x = null;
        _this.collectionChanged = null;
        _this.filter = null;
        _this.records = new ObservableCollection$1(ShapefileRecord.$, 0);
        var a = _this.records;
        a.collectionChanged = delegateCombine(a.collectionChanged, function (a, b) {
            if (_this.collectionChanged != null) {
                _this.collectionChanged(_this, b);
            }
        });
        return _this;
    }
    Object.defineProperty(ShapefileConverter.prototype, "name", {
        get: function () {
            return this._name;
        },
        set: function (a) {
            this._name = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ShapefileConverter.prototype, "worldRect", {
        get: function () {
            return this.a8;
        },
        set: function (a) {
            if (Rect.l_op_Inequality(this.a8, a)) {
                var b = this.a8;
                this.a8 = a;
                this.aw("WorldRect", b, this.a8);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ShapefileConverter.prototype, "computedWorldRect", {
        get: function () {
            return this.a7;
        },
        set: function (a) {
            if (Rect.l_op_Inequality(this.a7, a)) {
                var b = this.a7;
                this.a7 = a;
                this.aw("ComputedWorldRect", b, this.a7);
            }
        },
        enumerable: true,
        configurable: true
    });
    ShapefileConverter.prototype.getWorldBounds = function (a) {
        if (a && this.computedWorldRect.isEmpty == false) {
            return this.computedWorldRect;
        }
        return this.worldRect;
    };
    ShapefileConverter.prototype.setWorldBounds = function (a, b) {
        if (a) {
            this.computedWorldRect = b;
        }
        else {
            this.worldRect = b;
        }
    };
    Object.defineProperty(ShapefileConverter.prototype, "shapeType", {
        get: function () {
            return this.shapeHeader.a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ShapefileConverter.prototype, "shapeHeader", {
        get: function () {
            return this._shapeHeader;
        },
        set: function (a) {
            this._shapeHeader = a;
        },
        enumerable: true,
        configurable: true
    });
    ShapefileConverter.prototype.aw = function (a, b, c) {
        switch (a) {
            case ShapefileConverter.$$p[2]:
            case ShapefileConverter.$$p[0]:
                if (this.shapefileSource != null && this.databaseSource != null) {
                    this.at();
                }
                break;
        }
        this.j(a);
    };
    ShapefileConverter.prototype.sendImportCompleted = function () {
        if (this.deferImportCompleted && this.q) {
            this.deferImportCompleted = false;
            if (this.importCompleted != null) {
                this.importCompleted(this, new AsyncCompletedEventArgs(null, false, null));
            }
        }
    };
    ShapefileConverter.prototype.au = function (a) {
        this.q = true;
        if (this.deferImportCompleted) {
            if (this.importPending != null) {
                this.importPending(this, a);
            }
            return;
        }
        if (this.importCompleted != null) {
            this.importCompleted(this, a);
        }
    };
    ShapefileConverter.prototype.at = function () {
        var _this = this;
        this.q = false;
        var a = null;
        var b = null;
        var c = function () {
            if (a != null && b != null) {
                _this.av(a, b);
                _this.au(new AsyncCompletedEventArgs(null, false, null));
            }
        };
        BinaryFileDownloader.a(this.shapefileSource, function (d) {
            a = new BinaryReader(0, d, false);
            c();
        }, function (d) {
            throw new BaseError(1, "shape file download error: " + d);
        });
        BinaryFileDownloader.a(this.databaseSource, function (d) {
            b = new BinaryReader(0, d, false);
            c();
        }, function (d) {
            throw new BaseError(1, "dbf file download error: " + d);
        });
    };
    ShapefileConverter.prototype.av = function (a, b) {
        this.shapeHeader = ShapeFileUtil.a(a, b);
        this.worldRect = this.shapeHeader.e;
        var c = a.currentPosition;
        var d = a.length;
        var e = Rect.empty;
        while (c < d) {
            var f = ShapeFileUtil.e(this.shapeHeader, a, b);
            var record_ = f;
            var g = true;
            if (this.filter != null) {
                var h = new ShapeFilterRecordEventArgs();
                h.b = true;
                h.a = f;
                this.filter(this, h);
                if (!h.b) {
                    g = false;
                }
            }
            if (g) {
                this.records.add(f);
                if (e.isEmpty) {
                    e = f.bounds;
                }
                else {
                    e.union(f.bounds);
                }
            }
            c = a.currentPosition;
            d = a.length;
        }
        this.computedWorldRect = e.isEmpty ? this.worldRect : e;
    };
    Object.defineProperty(ShapefileConverter.prototype, "records", {
        get: function () {
            return this.x;
        },
        set: function (a) {
            this.x = a;
        },
        enumerable: true,
        configurable: true
    });
    ShapefileConverter.prototype.indexOf = function (a) {
        return this.x.indexOf(a);
    };
    ShapefileConverter.prototype.insert = function (a, b) {
        this.x.insert(a, b);
    };
    ShapefileConverter.prototype.removeAt = function (a) {
        this.x.removeAt(a);
    };
    ShapefileConverter.prototype.item = function (a, b) {
        if (arguments.length === 2) {
            this.x.item(a, b);
            return b;
        }
        else {
            return this.x._inner[a];
        }
    };
    ShapefileConverter.prototype.add = function (a) {
        this.x.add(a);
    };
    ShapefileConverter.prototype.clear = function () {
        this.x.clear();
    };
    ShapefileConverter.prototype.contains = function (a) {
        return this.x.contains(a);
    };
    ShapefileConverter.prototype.copyTo = function (a, b) {
        this.x.copyTo(a, b);
    };
    Object.defineProperty(ShapefileConverter.prototype, "count", {
        get: function () {
            return this.x.count;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ShapefileConverter.prototype, "isReadOnly", {
        get: function () {
            return this.x.isReadOnly;
        },
        enumerable: true,
        configurable: true
    });
    ShapefileConverter.prototype.remove = function (a) {
        return this.x.remove(a);
    };
    ShapefileConverter.prototype.getEnumerator = function () {
        return this.x.getEnumerator();
    };
    ShapefileConverter.prototype.getEnumeratorObject = function () {
        return this.getEnumerator();
    };
    ShapefileConverter.prototype.getPointData = function () {
        return new List$1(ShapefileRecord.$, 1, this.records);
    };
    ShapefileConverter.prototype.getRecordsCount = function () {
        if (this.records == null) {
            return 0;
        }
        return this.records.count;
    };
    ShapefileConverter.prototype.getRecord = function (a) {
        return this.records._inner[a];
    };
    ShapefileConverter.prototype.getRecordBounds = function (a) {
        if (this.records == null) {
            return Rect.empty;
        }
        if (a < 0 || a > this.records.count - 1) {
            return Rect.empty;
        }
        return this.records._inner[a].bounds;
    };
    ShapefileConverter.prototype.getRecordFieldNames = function (a) {
        if (this.records == null) {
            return null;
        }
        if (a < 0 || a > this.records.count - 1) {
            return null;
        }
        var b = this.records._inner[a];
        if (b == null || b.fieldsNames == null) {
            return null;
        }
        return b.fieldsNames.toArray();
    };
    ShapefileConverter.prototype.getRecordValue = function (a, b) {
        if (this.records == null) {
            return null;
        }
        if (a < 0 || a > this.records.count - 1) {
            return null;
        }
        var c = this.records._inner[a];
        if (c == null || c.fieldsNames == null) {
            return null;
        }
        return c.getFieldValue(b);
    };
    ShapefileConverter.prototype.removeRecord = function (a) {
        if (this.records == null) {
            return;
        }
        if (a < 0 || a > this.records.count - 1) {
            return;
        }
        this.records.removeAt(a);
    };
    ShapefileConverter.prototype.shiftAllShapes = function (a, b) {
        if (this.records == null) {
            return;
        }
        for (var c = 0; c < this.records.count; c++) {
            this.shiftShapes(c, a, b);
        }
    };
    ShapefileConverter.prototype.shiftShapes = function (a, b, c) {
        var e_1, _a, e_2, _b;
        if (this.records == null) {
            return;
        }
        if (a < 0 || a > this.records.count - 1) {
            return;
        }
        var d = new List$1(List$1.$.specialize(Point_$type), 0);
        var e = this.records._inner[a].points;
        try {
            for (var _c = tslib_1.__values(fromEnum(e)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var f = _d.value;
                var g = ShapefileConverter.w(f, b, c);
                try {
                    for (var _e = (e_2 = void 0, tslib_1.__values(fromEnum(g))), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var h = _f.value;
                        d.add(h);
                    }
                }
                catch (e_2_1) {
                    e_2 = { error: e_2_1 };
                }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return))
                            _b.call(_e);
                    }
                    finally {
                        if (e_2)
                            throw e_2.error;
                    }
                }
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
        this.records._inner[a].points = d;
    };
    ShapefileConverter.ai = function (a, b) {
        return ShapefileConverter.ag(a, b, -180, 180);
    };
    ShapefileConverter.ah = function (a, b) {
        return ShapefileConverter.ag(a, b, -85.05112878, 85.05112878);
    };
    ShapefileConverter.ag = function (a, b, c, d) {
        var e = a;
        if (isNaN_(b) || b == 0) {
            return e;
        }
        if (e + b < c) {
            e += 2 * d;
            e += b;
        }
        else if (e + b > d) {
            e -= 2 * d;
            e += b;
        }
        else {
            e += b;
        }
        return e;
    };
    ShapefileConverter.isValidGeoPoint = function (a) {
        if (a.x < -180 || a.x > 180) {
            return false;
        }
        if (a.y < -90 || a.y > 90) {
            return false;
        }
        return true;
    };
    ShapefileConverter.isValidGeoShape = function (a) {
        var e_3, _a;
        if (a.count < 3) {
            return false;
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                if (!ShapefileConverter.isValidGeoPoint(b)) {
                    return false;
                }
            }
        }
        catch (e_3_1) {
            e_3 = { error: e_3_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_3)
                    throw e_3.error;
            }
        }
        return true;
    };
    ShapefileConverter.w = function (a, b, c, d) {
        var e_4, _a, e_5, _b;
        if (d === void 0) {
            d = "";
        }
        var e = 1.7976931348623157E+308;
        var f = -1.7976931348623157E+308;
        var g = new List$1(Point_$type, 0);
        try {
            for (var _c = tslib_1.__values(fromEnum(a)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var h = _d.value;
                var i = ShapefileConverter.ag(h.x, b, -180, 180);
                var j = ShapefileConverter.ag(h.y, c, -85.05112878, 85.05112878);
                e = Math.min(e, i);
                f = Math.max(f, i);
                g.add({ $type: Point_$type, x: i, y: j });
            }
        }
        catch (e_4_1) {
            e_4 = { error: e_4_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_4)
                    throw e_4.error;
            }
        }
        var k = new List$1(List$1.$.specialize(Point_$type), 0);
        var l = ShapefileConverter.ba(g);
        var m = l.width;
        if (l.left >= -180 && l.right <= 180 && l.width > 180) {
            var n = new List$1(Point_$type, 0);
            var o = new List$1(Point_$type, 0);
            try {
                for (var _e = tslib_1.__values(fromEnum(g)), _f = _e.next(); !_f.done; _f = _e.next()) {
                    var p = _f.value;
                    if (p.x >= -180 && p.x < 0) {
                        n.add(p);
                    }
                    else {
                        o.add(p);
                    }
                }
            }
            catch (e_5_1) {
                e_5 = { error: e_5_1 };
            }
            finally {
                try {
                    if (_f && !_f.done && (_b = _e.return))
                        _b.call(_e);
                }
                finally {
                    if (e_5)
                        throw e_5.error;
                }
            }
            var q = ShapefileConverter.ba(n);
            var r = q.width;
            var s = ShapefileConverter.ba(o);
            var t = s.width;
            if (ShapefileConverter.isValidGeoShape(n)) {
                k.add(n);
            }
            if (ShapefileConverter.isValidGeoShape(o)) {
                k.add(o);
            }
        }
        else {
            if (ShapefileConverter.isValidGeoShape(g)) {
                k.add(g);
            }
        }
        return k;
    };
    ShapefileConverter.prototype.getMaxLongitude = function (a, b, c, d) {
        var e_6, _a, e_7, _b, e_8, _c;
        if (this.records == null) {
            return NaN;
        }
        if (a < 0 || a > this.records.count - 1) {
            return NaN;
        }
        var e = this.records._inner[a].points;
        var f = c;
        if (b) {
            var g = ShapefileConverter.v(e);
            try {
                for (var _d = tslib_1.__values(fromEnum(g)), _e = _d.next(); !_e.done; _e = _d.next()) {
                    var h = _e.value;
                    if (h.x < d) {
                        f = Math.max(f, h.x);
                    }
                }
            }
            catch (e_6_1) {
                e_6 = { error: e_6_1 };
            }
            finally {
                try {
                    if (_e && !_e.done && (_a = _d.return))
                        _a.call(_d);
                }
                finally {
                    if (e_6)
                        throw e_6.error;
                }
            }
        }
        else {
            try {
                for (var _f = tslib_1.__values(fromEnum(e)), _g = _f.next(); !_g.done; _g = _f.next()) {
                    var i = _g.value;
                    try {
                        for (var _h = (e_8 = void 0, tslib_1.__values(fromEnum(i))), _j = _h.next(); !_j.done; _j = _h.next()) {
                            var j = _j.value;
                            if (j.x < d) {
                                f = Math.max(f, j.x);
                            }
                        }
                    }
                    catch (e_8_1) {
                        e_8 = { error: e_8_1 };
                    }
                    finally {
                        try {
                            if (_j && !_j.done && (_c = _h.return))
                                _c.call(_h);
                        }
                        finally {
                            if (e_8)
                                throw e_8.error;
                        }
                    }
                }
            }
            catch (e_7_1) {
                e_7 = { error: e_7_1 };
            }
            finally {
                try {
                    if (_g && !_g.done && (_b = _f.return))
                        _b.call(_f);
                }
                finally {
                    if (e_7)
                        throw e_7.error;
                }
            }
        }
        return f;
    };
    ShapefileConverter.prototype.setRecordValue = function (a, b, c) {
        if (this.records == null) {
            return;
        }
        if (a < 0 || a > this.records.count - 1) {
            return;
        }
        var d = this.records._inner[a];
        if (d == null || d.fieldsNames == null) {
            return;
        }
        d.setFieldValue(b, c);
    };
    ShapefileConverter.prototype.getRecordValues = function (a) {
        if (this.records == null) {
            return null;
        }
        var b = new Array(this.records.count);
        for (var c = 0; c < this.records.count; c++) {
            var d = this.getRecordValue(c, a);
            b[c] = d;
        }
        return b;
    };
    ShapefileConverter.prototype.setRecordValues = function (a, b) {
        if (this.records == null) {
            return;
        }
        var c = Math.min(b.length, this.records.count);
        for (var d = 0; d < c; d++) {
            this.setRecordValue(d, a, b[d]);
        }
    };
    ShapefileConverter.ba = function (a) {
        var b = Number.POSITIVE_INFINITY;
        var c = Number.POSITIVE_INFINITY;
        var d = Number.NEGATIVE_INFINITY;
        var e = Number.NEGATIVE_INFINITY;
        for (var f = 0; f < a.count; f++) {
            var g = a._inner[f];
            b = Math.min(b, g.x);
            c = Math.min(c, g.y);
            d = Math.max(d, g.x);
            e = Math.max(e, g.y);
        }
        if (isInfinity(b) || isInfinity(c) || isInfinity(c) || isInfinity(e)) {
            return Rect.empty;
        }
        return new Rect(0, b, c, d - b, e - c);
    };
    ShapefileConverter.prototype.getLargestShapeBoundsForRecord = function (a) {
        if (this.records == null) {
            return Rect.empty;
        }
        if (a < 0 || a > this.records.count - 1) {
            return Rect.empty;
        }
        var b = this.records._inner[a];
        if (b == null || b.fieldsNames == null) {
            return Rect.empty;
        }
        var c = ShapefileConverter.v(b.points);
        return ShapefileConverter.ba(c);
    };
    ShapefileConverter.v = function (a) {
        var e_9, _a, e_10, _b;
        var b = new List$1(Point_$type, 0);
        var c = -1.7976931348623157E+308;
        try {
            for (var _c = tslib_1.__values(fromEnum(a)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var d = _d.value;
                var e = 1.7976931348623157E+308;
                var f = -1.7976931348623157E+308;
                var g = 1.7976931348623157E+308;
                var h = -1.7976931348623157E+308;
                try {
                    for (var _e = (e_10 = void 0, tslib_1.__values(fromEnum(d))), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var i = _f.value;
                        g = Math.min(g, i.y);
                        h = Math.max(h, i.y);
                        e = Math.min(e, i.x);
                        f = Math.max(f, i.x);
                    }
                }
                catch (e_10_1) {
                    e_10 = { error: e_10_1 };
                }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return))
                            _b.call(_e);
                    }
                    finally {
                        if (e_10)
                            throw e_10.error;
                    }
                }
                var j = f - e;
                var k = h - g;
                var l = new Rect(0, e, g, j, k);
                var m = l.width * l.height;
                if (m > c) {
                    c = m;
                    b = d;
                }
            }
        }
        catch (e_9_1) {
            e_9 = { error: e_9_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_9)
                    throw e_9.error;
            }
        }
        return b;
    };
    ShapefileConverter.$t = markType(ShapefileConverter, 'ShapefileConverter', DependencyObjectNotifier.$, [IList$1_$type.specialize(ShapefileRecord.$), INotifyCollectionChanged_$type]);
    ShapefileConverter.minLatitude = -85.05112878;
    ShapefileConverter.maxLatitude = 85.05112878;
    ShapefileConverter.minLongitude = -180;
    ShapefileConverter.maxLongitude = 180;
    ShapefileConverter.$$p = markDep(DependencyProperty, PropertyMetadata, ShapefileConverter, 'aw', ['DatabaseSource', [2, null], 'DeferImportCompleted', [0, false], 'ShapefileSource', [2, null]]);
    return ShapefileConverter;
}(DependencyObjectNotifier));
export { ShapefileConverter };
