import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UmbArrayState } from '../../../../libs/observable-api/index.js';
export class UmbStateManager extends UmbControllerBase {
    /**
     * Creates an instance of UmbStateManager.
     * @param {UmbControllerHost} host
     * @memberof UmbStateManager
     */
    constructor(host) {
        super(host);
        /**
         * Observable that emits all states in the state manager
         * @memberof UmbStateManager
         */
        this._states = new UmbArrayState([], (x) => x.unique);
        this.states = this._states.asObservable();
        /**
         * Observable that emits true if there are any states in the state manager
         * @memberof UmbStateManager
         */
        this.isOn = this._states.asObservablePart((x) => x.length > 0);
        /**
         * Observable that emits true if there are no states in the state manager
         * @memberof UmbStateManager
         */
        this.isOff = this._states.asObservablePart((x) => x.length === 0);
    }
    /**
     * Add a new state to the state manager
     * @param {StateType} state
     * @memberof UmbStateManager
     */
    addState(state) {
        if (!state.unique)
            throw new Error('State must have a unique property');
        if (this._states.getValue().find((x) => x.unique === state.unique)) {
            throw new Error('State with unique already exists');
        }
        this._states.setValue([...this._states.getValue(), state]);
    }
    /**
     * Add multiple states to the state manager
     * @param {StateType[]} states
     * @memberof UmbStateManager
     */
    addStates(states) {
        states.forEach((state) => this.addState(state));
    }
    /**
     * Remove a state from the state manager
     * @param {StateType['unique']} unique
     * @memberof UmbStateManager
     */
    removeState(unique) {
        this._states.removeOne(unique);
    }
    /**
     * Remove multiple states from the state manager
     * @param {StateType['unique'][]} uniques
     * @memberof UmbStateManager
     */
    removeStates(uniques) {
        this._states.remove(uniques);
    }
    /**
     * Get all states from the state manager
     * @returns {StateType[]} {StateType[]} All states in the state manager
     * @memberof UmbStateManager
     */
    getStates() {
        return this._states.getValue();
    }
    getIsOn() {
        return this._states.getValue().length > 0;
    }
    getIsOff() {
        return this._states.getValue().length === 0;
    }
    /**
     * Clear all states from the state manager
     * @memberof UmbStateManager
     */
    clear() {
        this._states.setValue([]);
    }
    destroy() {
        super.destroy();
        this._states.destroy();
    }
}
