// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Disableable } from '../../../Behaviors/Disableable';
import { DropDownable } from '../../../Behaviors/DropDownable';
import { Themeable } from '../../../Behaviors/Themeable';
import { Valueable } from '../../../Behaviors/Valueable';
import { ClickOutsideController } from '../../../Controllers/ClickOutsideController';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { Placement } from '../../../Types/Placement';
import { CustomElement } from '../../Abstracts/CustomElement';
import { FloatingElement } from '../../Primitives/Floating/FloatingElement';
import { PortalElement } from '../../Primitives/Portal/PortalElement';
import { ColorEditorElement } from '../ColorEditor/ColorEditorElement';
import { IColorPalette } from '../ColorEditor/IColorPalette';
import { ColorSwatchElement } from '../ColorSwatch/ColorSwatchElement';
import { colorPickerElementTemplate } from './ColorPickerElementTemplate';
import { colorPickerElementCosmopolitanStyle } from './Themes/ColorPickerElement.Cosmopolitan';
import { colorPickerElementJoyStyle } from './Themes/ColorPickerElement.Joy';
import { colorPickerElementMemphisStyle } from './Themes/ColorPickerElement.Memphis';
// #endregion
/**
 * Color Picker - A user interface component for selecting colors using the ColorEditor.
 *
 * @element mosaik-color-picker
 *
 * @description
 * The Color Picker component provides a dropdown interface for selecting colors using
 * the comprehensive ColorEditor component. It displays a color swatch that, when clicked,
 * opens a dropdown containing the full color editing experience.
 *
 * @category Media
 *
 * @fires colorChanged {ColorChangedEvent} - Fired when the color value changes.
 *
 * @slot - Default slot for custom trigger content.
 *
 * @csspart swatch - The color swatch trigger element.
 * @csspart popup - The floating dropdown container.
 * @csspart editor - The color editor element.
 *
 * @dependency {ColorEditorElement} - The Color Editor component.
 * @dependency {ColorSwatchElement} - The Color Swatch component.
 * @dependency {FloatingElement} - The Floating component.
 * @dependency {PortalElement} - The Portal component.
 *
 * @public
 */
let ColorPickerElement = class ColorPickerElement extends Themeable(DropDownable(Disableable(Valueable(CustomElement)))) {
    // #region Fields
    _clickOutsideController;
    _floatingElement;
    _value;
    _format;
    _showAlpha;
    _showInputs;
    _showPalette;
    _showEyeDropper;
    _showSwatch;
    _palettes;
    _selectedPalette;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._value = '#ff0000';
        this._format = 'hex';
        this._showAlpha = true;
        this._showInputs = true;
        this._showPalette = true;
        this._showEyeDropper = false;
        this._showSwatch = true;
        this._palettes = IColorPalette.allPalettes();
        this._selectedPalette = IColorPalette.MATERIAL_PALETTE.title;
        this._clickOutsideController = new ClickOutsideController(this, {
            skipInitial: true,
            ignored: () => [this._floatingElement],
            callback: () => this.onClickedOutside()
        });
        // Set dropdown defaults
        Object.assign(this, {
            dropDownPlacement: Placement.BottomStart,
            dropDownMaxHeight: 'auto',
            dropDownWidth: 'auto'
        });
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-color-picker';
    }
    /**
     * Gets or sets the `value` property.
     *
     * @public
     * @override
     */
    get value() {
        return this._value;
    }
    set value(value) {
        if (this._value !== value) {
            this._value = value;
            this.requestUpdate('value');
        }
    }
    /**
     * Gets or sets the color format for the inputs.
     *
     * @public
     */
    get format() {
        return this._format;
    }
    set format(value) {
        if (this._format !== value) {
            this._format = value;
            this.requestUpdate('format');
        }
    }
    /**
     * Gets or sets whether to show the alpha slider and input.
     *
     * @public
     */
    get showAlpha() {
        return this._showAlpha;
    }
    set showAlpha(value) {
        if (this._showAlpha !== value) {
            this._showAlpha = value;
            this.requestUpdate('showAlpha');
        }
    }
    /**
     * Gets or sets whether to show the color inputs.
     *
     * @public
     */
    get showInputs() {
        return this._showInputs;
    }
    set showInputs(value) {
        if (this._showInputs !== value) {
            this._showInputs = value;
            this.requestUpdate('showInputs');
        }
    }
    /**
     * Gets or sets whether to show the color palette.
     *
     * @public
     */
    get showPalette() {
        return this._showPalette;
    }
    set showPalette(value) {
        if (this._showPalette !== value) {
            this._showPalette = value;
            this.requestUpdate('showPalette');
        }
    }
    /**
     * Gets or sets whether to show the eye dropper button.
     *
     * @public
     */
    get showEyeDropper() {
        return this._showEyeDropper;
    }
    set showEyeDropper(value) {
        if (this._showEyeDropper !== value) {
            this._showEyeDropper = value;
            this.requestUpdate('showEyeDropper');
        }
    }
    /**
     * Gets or sets whether to show the color swatch preview in the editor.
     *
     * @public
     */
    get showSwatch() {
        return this._showSwatch;
    }
    set showSwatch(value) {
        if (this._showSwatch !== value) {
            this._showSwatch = value;
            this.requestUpdate('showSwatch');
        }
    }
    /**
     * Gets or sets the available color palettes.
     *
     * @public
     */
    get palettes() {
        return this._palettes;
    }
    set palettes(value) {
        if (this._palettes !== value) {
            this._palettes = value;
            this.requestUpdate('palettes');
        }
    }
    /**
     * Gets or sets the currently selected palette name.
     *
     * @public
     */
    get selectedPalette() {
        return this._selectedPalette;
    }
    set selectedPalette(value) {
        if (this._selectedPalette !== value) {
            this._selectedPalette = value;
            this.requestUpdate('selectedPalette');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        this.on('click', (event) => {
            const target = event.target;
            // Only toggle if clicking on the swatch or host, not the editor
            if (target.closest('mosaik-color-editor')) {
                return;
            }
            this.toggle();
        });
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._floatingElement = this.getTemplatePart('popup');
    }
    /**
     * Handles changes from the color editor.
     *
     * @private
     * @template
     */
    onEditorChange(event) {
        event.stopPropagation();
        this.value = event.detail.color;
        this.emit('colorChanged', { detail: this.value });
    }
    /**
     * @protected
     */
    onIsDropDownOpenPropertyChanged(_prev, next) {
        if (next) {
            if (this.disabled) {
                this.close();
                return;
            }
            this._clickOutsideController.observe();
        }
        else {
            this._clickOutsideController.unobserve();
        }
    }
    /**
     * @private
     */
    onClickedOutside() {
        this.close();
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], ColorPickerElement.prototype, "value", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ColorPickerElement.prototype, "format", null);
__decorate([
    Property({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ColorPickerElement.prototype, "showAlpha", null);
__decorate([
    Property({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ColorPickerElement.prototype, "showInputs", null);
__decorate([
    Property({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ColorPickerElement.prototype, "showPalette", null);
__decorate([
    Property({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ColorPickerElement.prototype, "showEyeDropper", null);
__decorate([
    Property({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ColorPickerElement.prototype, "showSwatch", null);
__decorate([
    Property({ type: Array }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], ColorPickerElement.prototype, "palettes", null);
__decorate([
    Attribute({
        type: String,
        attribute: 'selected-palette'
    }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ColorPickerElement.prototype, "selectedPalette", null);
__decorate([
    Watch('isDropDownOpen', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], ColorPickerElement.prototype, "onIsDropDownOpenPropertyChanged", null);
ColorPickerElement = __decorate([
    Component({
        selector: 'mosaik-color-picker',
        template: colorPickerElementTemplate,
        themes: {
            joy: colorPickerElementJoyStyle,
            memphis: colorPickerElementMemphisStyle,
            cosmopolitan: colorPickerElementCosmopolitanStyle
        },
        imports: [
            ColorEditorElement,
            ColorSwatchElement,
            FloatingElement,
            PortalElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ColorPickerElement);
export { ColorPickerElement };
//# sourceMappingURL=ColorPickerElement.js.map