"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "createUniqueMap", {
    enumerable: true,
    get: function() {
        return createUniqueMap;
    }
});
const _function = require("../../helpers/function");
const _errors = require("../../helpers/errors");
const DEFAULT_ERROR_ID_EXISTS = (id)=>`The id '${id}' is already declared in a map.`;
function createUniqueMap({ errorIdExists } = {}) {
    const uniqueMap = new Map();
    errorIdExists = (0, _function.isFunction)(errorIdExists) ? errorIdExists : DEFAULT_ERROR_ID_EXISTS;
    /**
   * Adds a new item to the unique map. Throws error if `id` is already added.
   *
   * @param {*} id The ID of the adding item.
   * @param {*} item The adding item.
   */ function addItem(id, item) {
        if (hasItem(id)) {
            (0, _errors.throwWithCause)(errorIdExists(id));
        }
        uniqueMap.set(id, item);
    }
    /**
   * Removes item from the passed id if exists.
   *
   * @param {*} id The ID to remove.
   * @returns {boolean}
   */ function removeItem(id) {
        return uniqueMap.delete(id);
    }
    /**
   * Clears the map.
   */ function clear() {
        uniqueMap.clear();
    }
    /**
   * Returns ID for the passed item.
   *
   * @param {*} item The item of the getting ID.
   * @returns {*}
   */ function getId(item) {
        const [itemId] = getItems().find(([id, element])=>{
            if (item === element) {
                return id;
            }
            return false;
        }) || [
            null
        ];
        return itemId;
    }
    /**
   * Returns item from the passed ID.
   *
   * @param {*} id The ID of the getting item.
   * @returns {*}
   */ function getItem(id) {
        return uniqueMap.get(id);
    }
    /**
   * Gets all items from the map.
   *
   * @returns {Array}
   */ function getItems() {
        return [
            ...uniqueMap
        ];
    }
    /**
   * Gets all values from the map.
   *
   * @returns {Array}
   */ function getValues() {
        return [
            ...uniqueMap.values()
        ];
    }
    /**
   * Verifies if the passed ID exists in a map.
   *
   * @param {*} id The ID to check if registered.
   * @returns {boolean}
   */ function hasItem(id) {
        return uniqueMap.has(id);
    }
    return {
        addItem,
        clear,
        getId,
        getItem,
        getItems,
        getValues,
        hasItem,
        removeItem
    };
}
