var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { r as registerInstance, h } from './chunk-eb0263ab.js';
import { _ as _t } from './chunk-a41b3114.js';
import { g as getMap } from './chunk-caeafbad.js';
var FeatureList = /** @class */ (function () {
    function FeatureList(hostRef) {
        registerInstance(this, hostRef);
        this.data = {};
        this.batchSize = 20;
        this.display = 'visible';
        this.features = [];
        this.item = true;
        this.loadingSpinner = 'bubbles';
        this.loadingText = _t('webmapgl.feature-list.loading');
        this.order = 'asc';
    }
    FeatureList.prototype.componentWillLoad = function () {
        var _this = this;
        this.handleSourceId();
        var map = this.getMap();
        if (map)
            map.on('moveend', function () { return _this.filter(); });
    };
    FeatureList.prototype.handleInfiniteScroll = function (e) {
        this.limit += this.batchSize;
        e.target.complete();
    };
    FeatureList.prototype.handleStyleUpdated = function (e) {
        if (e.detail.id === this.styleId)
            this.handleSourceId();
    };
    FeatureList.prototype.handleSourceId = function () {
        return __awaiter(this, void 0, void 0, function () {
            var style, data, res, _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        style = document.querySelector("#" + this.styleId);
                        if (!style || !style.json.sources)
                            return [2 /*return*/];
                        data = style.json.sources[this.sourceId].data;
                        if (!(typeof data === 'string')) return [3 /*break*/, 3];
                        return [4 /*yield*/, fetch(data)];
                    case 1:
                        res = _b.sent();
                        _a = this;
                        return [4 /*yield*/, res.json()];
                    case 2:
                        _a.data = _b.sent();
                        return [3 /*break*/, 4];
                    case 3:
                        this.data = data;
                        _b.label = 4;
                    case 4:
                        this.filter();
                        return [2 /*return*/];
                }
            });
        });
    };
    FeatureList.prototype.getMap = function () {
        return getMap(this.mapId).map;
    };
    FeatureList.prototype.filter = function () {
        return __awaiter(this, void 0, void 0, function () {
            var features, bounds_1;
            return __generator(this, function (_a) {
                if (!this.data.features)
                    return [2 /*return*/];
                if (this.display === 'all') {
                    features = this.data.features;
                }
                else {
                    bounds_1 = this.getMap().getBounds();
                    features = this.data.features.filter(function (feature) {
                        var _a = feature.geometry.coordinates, lng = _a[0], lat = _a[1];
                        return lng >= bounds_1.getWest() && lng <= bounds_1.getEast() &&
                            lat >= bounds_1.getSouth() && lat <= bounds_1.getNorth();
                    });
                }
                this.limit = this.batchSize;
                this.features = this.sort(features);
                return [2 /*return*/];
            });
        });
    };
    FeatureList.prototype.sort = function (features) {
        var _this = this;
        if (this.order === 'none' || !this.orderBy)
            return features;
        features.sort(function (aFeature, bFeature) {
            var a = aFeature.properties[_this.orderBy];
            var b = bFeature.properties[_this.orderBy];
            if (a < b)
                return (_this.order === 'asc') ? -1 : 1;
            if (a > b)
                return (_this.order === 'asc') ? 1 : -1;
            return 0;
        });
        return features;
    };
    FeatureList.prototype.render = function () {
        var _this = this;
        var ComponentTag = this.component;
        var features = (this.limit) ?
            this.features.slice(0, this.limit) : this.features;
        var hasMore = features.length < this.features.length;
        var items = features.map(function (feature) {
            var component = h(ComponentTag, Object.assign({ feature: feature }, _this.componentOptions));
            return (_this.item) ? h("ion-item", null, component) : component;
        });
        return ([
            h("ion-list", null, h("slot", { name: "start" }), items, h("slot", { name: "end" })),
            h("ion-infinite-scroll", { disabled: !hasMore }, h("ion-infinite-scroll-content", { "loading-spinner": this.loadingSpinner, "loading-text": this.loadingText }))
        ]);
    };
    Object.defineProperty(FeatureList, "watchers", {
        get: function () {
            return {
                "styleId": ["handleSourceId"],
                "sourceId": ["handleSourceId"]
            };
        },
        enumerable: true,
        configurable: true
    });
    return FeatureList;
}());
export { FeatureList as gl_feature_list };
