var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { AcTrLinearSpatialIndex } from './AcTrLinearSpatialIndex';
import { AcTrRBushSpatialIndex } from './AcTrRBushSpatialIndex';
/**
 * A two-level (hierarchical) spatial index designed for complex CAD
 * scene structures such as blocks, groups, layers, or nested entities.
 *
 * The index consists of:
 *
 * 1. A first-level spatial index that stores coarse bounding boxes
 *    (AcEdSpatialQueryResultItem) and maps each result to an `id`.
 *
 * 2. A second-level map from `id` to another spatial index, which
 *    contains more detailed spatial data for that specific entity,
 *    block, or group.
 *
 * Spatial queries are executed in two phases:
 * - First, the query is performed against the root spatial index to
 *   find candidate items.
 * - Then, for each candidate, the corresponding second-level spatial
 *   index (if present) is queried for more precise results.
 * - Results from both levels are merged into a single query result.
 *
 * This design allows:
 * - Mixing different spatial index implementations (e.g. R-tree and
 *   linear scan) at different hierarchy levels
 * - Efficient querying of large, nested CAD datasets
 * - Lazy or selective construction of fine-grained spatial indexes
 *
 * This class is particularly suitable for CAD viewers and editors
 * where entities are grouped hierarchically but still require fast
 * spatial queries such as selection, picking, and hit-testing.
 */
var AcTrHierarchicalSpatialIndex = /** @class */ (function () {
    function AcTrHierarchicalSpatialIndex(rootIndex) {
        this.childIndexes = new Map();
        this.rootIndex = rootIndex !== null && rootIndex !== void 0 ? rootIndex : new AcTrRBushSpatialIndex();
    }
    /** Register second-level index for an id */
    AcTrHierarchicalSpatialIndex.prototype.setChildIndex = function (id, index) {
        this.childIndexes.set(id, index);
    };
    /** Remove second-level index */
    AcTrHierarchicalSpatialIndex.prototype.removeChildIndex = function (id) {
        this.childIndexes.delete(id);
    };
    AcTrHierarchicalSpatialIndex.prototype.insert = function (item) {
        var _a;
        this.rootIndex.insert(item);
        (_a = this.childIndexes.get(item.id)) === null || _a === void 0 ? void 0 : _a.insert(item);
    };
    AcTrHierarchicalSpatialIndex.prototype.load = function (items) {
        this.rootIndex.load(items);
    };
    AcTrHierarchicalSpatialIndex.prototype.remove = function (item, equals) {
        this.rootIndex.remove(item, equals);
        this.childIndexes.delete(item.id);
    };
    AcTrHierarchicalSpatialIndex.prototype.removeById = function (id) {
        this.rootIndex.removeById(id);
        this.childIndexes.delete(id);
    };
    AcTrHierarchicalSpatialIndex.prototype.clear = function () {
        this.rootIndex.clear();
        this.childIndexes.forEach(function (i) { return i.clear(); });
        this.childIndexes.clear();
    };
    AcTrHierarchicalSpatialIndex.prototype.search = function (bbox) {
        var e_1, _a;
        var level1 = this.rootIndex.search(bbox);
        var result = [];
        try {
            for (var level1_1 = __values(level1), level1_1_1 = level1_1.next(); !level1_1_1.done; level1_1_1 = level1_1.next()) {
                var hit = level1_1_1.value;
                var child = this.childIndexes.get(hit.id);
                if (!child) {
                    result.push(hit);
                    continue;
                }
                var level2 = child.search(bbox);
                result.push(__assign(__assign({}, hit), { children: level2 }));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (level1_1_1 && !level1_1_1.done && (_a = level1_1.return)) _a.call(level1_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return result;
    };
    AcTrHierarchicalSpatialIndex.prototype.collides = function (bbox) {
        var _this = this;
        if (!this.rootIndex.collides(bbox))
            return false;
        var level1 = this.rootIndex.search(bbox);
        return level1.some(function (hit) {
            var child = _this.childIndexes.get(hit.id);
            return child ? child.collides(bbox) : true;
        });
    };
    AcTrHierarchicalSpatialIndex.prototype.all = function () {
        var e_2, _a;
        var result = [];
        try {
            for (var _b = __values(this.rootIndex.all()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var hit = _c.value;
                var child = this.childIndexes.get(hit.id);
                if (child)
                    result.push.apply(result, __spreadArray([], __read(child.all()), false));
                else
                    result.push(hit);
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return result;
    };
    AcTrHierarchicalSpatialIndex.prototype.createChildIndex = function (group) {
        var size = group.boxes.length;
        var spatialIndex = undefined;
        if (size > AcTrHierarchicalSpatialIndex.THRESHOLD) {
            spatialIndex = new AcTrRBushSpatialIndex();
        }
        else if (size > 0) {
            spatialIndex = new AcTrLinearSpatialIndex();
        }
        if (spatialIndex) {
            group.boxes.forEach(function (box) {
                spatialIndex.insert(box);
            });
            this.setChildIndex(group.objectId, spatialIndex);
        }
        return spatialIndex;
    };
    AcTrHierarchicalSpatialIndex.THRESHOLD = 100;
    return AcTrHierarchicalSpatialIndex;
}());
export { AcTrHierarchicalSpatialIndex };
//# sourceMappingURL=AcTrHierarchicalSpatialIndex.js.map