"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get MapCollection () {
        return MapCollection;
    },
    get getRegisteredMapsCounter () {
        return getRegisteredMapsCounter;
    }
});
const _mixed = require("../../helpers/mixed");
const _object = require("../../helpers/object");
const _localHooks = /*#__PURE__*/ _interop_require_default(require("../../mixins/localHooks"));
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
// Counter for checking if there is a memory leak.
let registeredMaps = 0;
class MapCollection {
    /**
   * Register custom index map.
   *
   * @param {string} uniqueName Unique name of the index map.
   * @param {IndexMap} indexMap Index map containing miscellaneous (i.e. Meta data, indexes sequence), updated after remove and insert data actions.
   */ register(uniqueName, indexMap) {
        if (this.collection.has(uniqueName) === false) {
            this.collection.set(uniqueName, indexMap);
            indexMap.addLocalHook('change', ()=>this.runLocalHooks('change', indexMap));
            registeredMaps += 1;
        }
    }
    /**
   * Unregister custom index map.
   *
   * @param {string} name Name of the index map.
   */ unregister(name) {
        const indexMap = this.collection.get(name);
        if ((0, _mixed.isDefined)(indexMap)) {
            indexMap.destroy();
            this.collection.delete(name);
            this.runLocalHooks('change', indexMap);
            registeredMaps -= 1;
        }
    }
    /**
   * Unregisters and destroys all collected index map instances.
   */ unregisterAll() {
        this.collection.forEach((indexMap, name)=>this.unregister(name));
        this.collection.clear();
    }
    /**
   * Get index map for the provided name.
   *
   * @param {string} [name] Name of the index map.
   * @returns {Array|IndexMap}
   */ get(name) {
        if ((0, _mixed.isUndefined)(name)) {
            return Array.from(this.collection.values());
        }
        return this.collection.get(name);
    }
    /**
   * Get collection size.
   *
   * @returns {number}
   */ getLength() {
        return this.collection.size;
    }
    /**
   * Remove some indexes and corresponding mappings and update values of the others within all collection's index maps.
   *
   * @private
   * @param {Array} removedIndexes List of removed indexes.
   */ removeFromEvery(removedIndexes) {
        this.collection.forEach((indexMap)=>{
            indexMap.remove(removedIndexes);
        });
    }
    /**
   * Insert new indexes and corresponding mapping and update values of the others all collection's index maps.
   *
   * @private
   * @param {number} insertionIndex Position inside the actual list.
   * @param {Array} insertedIndexes List of inserted indexes.
   */ insertToEvery(insertionIndex, insertedIndexes) {
        this.collection.forEach((indexMap)=>{
            indexMap.insert(insertionIndex, insertedIndexes);
        });
    }
    /**
   * Set default values to index maps within collection.
   *
   * @param {number} length Destination length for all stored maps.
   */ initEvery(length) {
        this.collection.forEach((indexMap)=>{
            indexMap.init(length);
        });
    }
    constructor(){
        /**
   * Collection of index maps.
   *
   * @type {Map<string, IndexMap>}
   */ this.collection = new Map();
    }
}
(0, _object.mixin)(MapCollection, _localHooks.default);
function getRegisteredMapsCounter() {
    return registeredMaps;
}
