// The MIT License (MIT)
//
// Copyright (c) 2020-2024 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import { BehaviorSubject } from 'rxjs';
/**
 * Manage the panels of the DesktopCanvas.
 *
 * Example of usage:
 * ```js
 *    (window as any).gmfapi.store.panels.openToolPanel('name');
 *    (window as any).gmfapi.store.panels.openFooterPanel('name');
 *    (window as any).gmfapi.store.panels.closeToolPanel();
 *    (window as any).gmfapi.store.panels.closeFooterPanel();
 * ```
 */
export class PanelsModel {
    constructor() {
        /**
         * The observable active tool panel.
         *
         * @private
         */
        this.activeToolPanel_ = new BehaviorSubject(null);
        /**
         * The observable active footer panel.
         *
         * @private
         */
        this.activeFooterPanel_ = new BehaviorSubject(null);
        /**
         * The observable active panels properties.
         *
         * @private
         */
        this.filterActive_ = new BehaviorSubject(false);
        /**
         * The current state of active tool panel.
         *
         * @private
         */
        this.activeToolPanelState_ = null;
        /**
         * The current state of active footer panel.
         *
         * @private
         */
        this.activeFooterPanelState_ = null;
        /**
         * Close the footer panel on tool panel change?
         *
         * @private
         */
        this.autoCloseFooterPanel_ = false;
    }
    /**
     * @returns the behavior about the active panel in the tool.
     */
    getActiveToolPanel() {
        return this.activeToolPanel_;
    }
    /**
     * @returns the behavior about the active panel in the footer.
     */
    getActiveFooterPanel() {
        return this.activeFooterPanel_;
    }
    /**
     * Experimental.
     *
     * @returns the behavior about the active filter.
     */
    getFilterActive() {
        return this.filterActive_;
    }
    /**
     * Open a panel, or close if already open
     *
     * @param panel the panel
     * @param options the options
     */
    openToolPanel(panel, options) {
        options = options || {};
        let state = options.state;
        if (state === undefined) {
            state = this.activeToolPanelState_ != panel;
        }
        else if (state && this.activeToolPanelState_ == panel) {
            return;
        }
        else if (!state && this.activeToolPanelState_ != panel) {
            if (options.noError !== true) {
                throw new Error("You try to close a tool panel that's not open");
            }
        }
        this.activeToolPanelState_ = state ? panel : null;
        this.activeToolPanel_.next(this.activeToolPanelState_);
        if (this.autoCloseFooterPanel_ && this.activeFooterPanelState_) {
            this.activeFooterPanelState_ = null;
            this.activeFooterPanel_.next(null);
        }
    }
    /**
     * Open a panel, or close if already open
     *
     * @param panel the panel
     * @param options the options
     */
    openFooterPanel(panel, options) {
        options = options || {};
        let state = options.state;
        if (state === undefined) {
            state = this.activeFooterPanelState_ != panel;
        }
        else if (state && this.activeFooterPanelState_ == panel) {
            return;
        }
        else if (!state && this.activeFooterPanelState_ != panel) {
            if (options.noError !== true) {
                throw new Error("You try to close a footer panel that's not open");
            }
        }
        this.activeFooterPanelState_ = state ? panel : null;
        this.activeFooterPanel_.next(this.activeFooterPanelState_);
        this.autoCloseFooterPanel_ = options.autoClose === true;
    }
    /**
     * Close the tool panel.
     */
    closeToolPanel() {
        if (this.activeToolPanelState_) {
            this.openToolPanel(this.activeToolPanelState_, { state: false });
        }
    }
    /**
     * Close the footer panel.
     */
    closeFooterPanel() {
        if (this.activeFooterPanelState_) {
            this.openFooterPanel(this.activeFooterPanelState_, { state: false });
        }
    }
    /**
     * Set if there is some active filter.
     *
     * Experimental.
     *
     * @param active is active.
     */
    setFilterActive(active) {
        this.filterActive_.next(active);
    }
}
const panels = new PanelsModel();
export default panels;
//# sourceMappingURL=panels.js.map