"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SelectedItemsController", {
    enumerable: true,
    get: function() {
        return SelectedItemsController;
    }
});
const _object = require("../../../helpers/object");
const _utils = require("../utils/utils");
class SelectedItemsController {
    /**
   * Sets the maximum number of selections.
   *
   * @param {number} maxSelectionsCount Maximum number of selections.
   */ setMaxSelectionCount(maxSelectionsCount) {
        this.maxSelectionsCount = maxSelectionsCount;
    }
    /**
   * Adds selected values (single or array) to the set.
   *
   * @param {string|object|Array<string|object>} items Items to add.
   */ add(items) {
        if (this.selectedItems.size >= this.maxSelectionsCount) {
            return;
        }
        if (Array.isArray(items)) {
            items.forEach((item)=>this.selectedItems.add(item));
        } else {
            this.selectedItems.add(items);
        }
    }
    /**
   * Removes a selected value from the set.
   *
   * @param {string|object} item Item to remove.
   * @returns {boolean} True if the item was removed, false otherwise.
   */ remove(item) {
        if ((0, _object.isKeyValueObject)(item)) {
            const itemsArray = Array.from(this.selectedItems);
            const foundItem = itemsArray.find((selectedItem)=>(0, _object.isObjectEqual)(selectedItem, item));
            if (foundItem !== undefined) {
                this.selectedItems.delete(foundItem);
                return true;
            }
        } else {
            return this.selectedItems.delete(item);
        }
        return false;
    }
    /**
   * Checks if a value is in the set.
   *
   * @param {*} value Value to check.
   * @returns {boolean}
   */ has(value) {
        return (0, _utils.includesValue)(this.getItemsArray(), value);
    }
    /**
   * Clears the set.
   */ clear() {
        this.selectedItems.clear();
    }
    /**
   * Gets the number of selected items.
   *
   * @returns {number}
   */ getSize() {
        return this.selectedItems.size;
    }
    /**
   * Gets the elements of the set.
   *
   * @returns {Array<*>}
   */ getItemsArray() {
        return Array.from(this.selectedItems);
    }
    /**
   * Creates a new SelectedItemsController.
   *
   * @param {Array<*>|undefined} selectedItems Array of selected items.
   */ constructor(selectedItems){
        /**
   * Set of selected items.
   *
   * @private
   * @type {Set<*>}
   */ this.selectedItems = this.selectedItems ?? new Set();
        /**
   * Maximum number of selections.
   *
   * @private
   * @type {number}
   */ this.maxSelectionsCount = Infinity;
        if (Array.isArray(selectedItems)) {
            this.selectedItems = new Set(selectedItems);
        } else {
            this.selectedItems = new Set();
        }
    }
}
