// #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 { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Emit } from '../../../../events/Decorators/EmitDecorator';
import { Disableable } from '../../../Behaviors/Disableable';
import { Focusable } from '../../../Behaviors/Focusable';
import { Themeable } from '../../../Behaviors/Themeable';
import { ResizeController } from '../../../Controllers/ResizeController';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { colorEditorAlphaSliderElementTemplate } from './ColorEditorAlphaSliderElementTemplate';
import { colorEditorAlphaSliderElementCosmopolitanStyle } from './Themes/ColorEditorAlphaSliderElement.Cosmopolitan';
import { colorEditorAlphaSliderElementJoyStyle } from './Themes/ColorEditorAlphaSliderElement.Joy';
import { colorEditorAlphaSliderElementMemphisStyle } from './Themes/ColorEditorAlphaSliderElement.Memphis';
// #endregion
/**
 * Color Editor Alpha Slider - A slider for selecting alpha/opacity values.
 *
 * @description
 * The Color Editor Alpha Slider component provides a horizontal slider for selecting
 * alpha/opacity values (0-1). It displays a checkerboard pattern beneath the gradient
 * to indicate transparency, with the current color fading from fully transparent to
 * fully opaque.
 *
 * @name ColorEditorAlphaSlider
 * @element mosaik-color-editor-alpha-slider
 * @category Media
 *
 * @csspart checkerboard - The checkerboard part.
 * @csspart gradient - The alpha gradient overlay showing transparency
 * @csspart thumb - The draggable indicator showing current alpha position
 *
 * @cssprop {String} --color-editor-alpha-slider-background-color - The editor alpha slider background color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-border-color - The editor alpha slider border color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-border-radius - The editor alpha slider border radius CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-border-style - The editor alpha slider border style CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-border-width - The editor alpha slider border width CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-checkerboard-size - The editor alpha slider checkerboard size CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-focus-ring-active-width - The editor alpha slider focus ring active width CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-focus-ring-color - The editor alpha slider focus ring color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-focus-ring-inward-offset - The editor alpha slider focus ring inward offset CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-focus-ring-outward-offset - The editor alpha slider focus ring outward offset CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-family - The editor alpha slider font family CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-letter-spacing - The editor alpha slider font letter spacing CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-line-height - The editor alpha slider font line height CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-size - The editor alpha slider font size CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-text-decoration - The editor alpha slider font text decoration CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-text-transform - The editor alpha slider font text transform CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-font-weight - The editor alpha slider font weight CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-foreground-color - The editor alpha slider foreground color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-gap - The editor alpha slider gap CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-height - The editor alpha slider height CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-padding-bottom - The editor alpha slider padding bottom CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-padding-left - The editor alpha slider padding left CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-padding-right - The editor alpha slider padding right CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-padding-top - The editor alpha slider padding top CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow - The editor alpha slider shadow CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow-blur - The editor alpha slider shadow blur CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow-color - The editor alpha slider shadow color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow-offset-x - The editor alpha slider shadow offset x CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow-offset-y - The editor alpha slider shadow offset y CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-shadow-spread - The editor alpha slider shadow spread CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-background-color - The editor alpha slider thumb background color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-border-color - The editor alpha slider thumb border color CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-border-radius - The editor alpha slider thumb border radius CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-border-width - The editor alpha slider thumb border width CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-shadow - The editor alpha slider thumb shadow CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-thumb-size - The editor alpha slider thumb size CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-transition-duration - The editor alpha slider transition duration CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-transition-mode - The editor alpha slider transition mode CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-transition-property - The editor alpha slider transition property CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-translate - The editor alpha slider translate CSS custom property.
 * @cssprop {String} --color-editor-alpha-slider-width - The editor alpha slider width CSS custom property.
 *
 * @fires alphaChanged {AlphaChangedEvent} - Fired when the alpha value changes via user interaction
 *
 * @example
 * Basic usage:
 * ```html
 * <mosaik-color-editor-alpha-slider></mosaik-color-editor-alpha-slider>
 * ```
 *
 * @example
 * With initial color and alpha value:
 * ```html
 * <mosaik-color-editor-alpha-slider color="#4a90d9" alpha="0.5"></mosaik-color-editor-alpha-slider>
 * ```
 *
 * @public
 */
let ColorEditorAlphaSliderElement = class ColorEditorAlphaSliderElement extends Themeable(Disableable(Focusable(CustomElement))) {
    // #region Fields
    _resizeController;
    _alphaChanged;
    _alpha;
    _color;
    _step;
    _boundingClientRect;
    _pointerDown;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the ColorEditorAlphaSliderElement class.
     *
     * @public
     */
    constructor() {
        super();
        this._alphaChanged = new HTMLElementEventEmitter(this, 'alphaChanged');
        this._alpha = 1;
        this._color = '#ff0000';
        this._step = 0.01;
        this._pointerDown = false;
        this._resizeController = new ResizeController(this, {
            callback: (entries) => {
                for (const entry of entries) {
                    this._boundingClientRect = entry.contentRect;
                }
            }
        });
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `is` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-color-editor-alpha-slider';
    }
    /**
     * Gets or sets the alpha value (0-1).
     *
     * @public
     * @attr
     */
    get alpha() {
        return this._alpha;
    }
    set alpha(value) {
        if (this._alpha !== value) {
            this._alpha = Math.max(0, Math.min(1, value));
            this.requestUpdate('alpha');
        }
    }
    /**
     * Gets or sets the base color for the gradient.
     *
     * @public
     * @attr
     */
    get color() {
        return this._color;
    }
    set color(value) {
        if (this._color !== value) {
            this._color = value;
            this.requestUpdate('color');
        }
    }
    /**
     * Gets or sets the step increment for the slider.
     *
     * @public
     */
    get step() {
        return this._step;
    }
    set step(value) {
        if (this._step !== value) {
            this._step = Math.max(0.001, value);
            this.requestUpdate('step');
        }
    }
    /**
     * Called when the alpha value changes.
     * Provides reference to `IAlphaChangedEventDetail` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get alphaChanged() {
        return this._alphaChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @inheritdoc
     * @public
     */
    connectedCallback() {
        super.connectedCallback();
        this.on('pointerdown', (e) => this.onPointerDown(e));
        this.on('pointermove', (e) => this.onPointerMove(e));
        this.on('pointerup', (e) => this.onPointerUp(e));
        this.on('pointercancel', (e) => this.onPointerUp(e));
        this.on('keydown', (e) => this.onKeyDown(e));
    }
    /**
     * @inheritdoc
     * @protected
     */
    firstUpdated(changedProperties) {
        super.firstUpdated(changedProperties);
        this._boundingClientRect = this.getBoundingClientRect();
    }
    /**
     * @inheritdoc
     * @protected
     */
    updated(changedProperties) {
        super.updated(changedProperties);
        this.setAttribute('aria-valuenow', Math.round(this._alpha * 100).toString());
    }
    /**
     * Handles pointer down events to start dragging.
     *
     * @private
     */
    onPointerDown(event) {
        if (this.disabled) {
            return;
        }
        this._pointerDown = true;
        this.setPointerCapture(event.pointerId);
        this.updateAlphaFromPointer(event);
    }
    /**
     * Handles pointer move events for dragging.
     *
     * @private
     */
    onPointerMove(event) {
        if (!this._pointerDown || this.disabled) {
            return;
        }
        this.updateAlphaFromPointer(event);
    }
    /**
     * Handles pointer up events to stop dragging.
     *
     * @private
     */
    onPointerUp(event) {
        if (this._pointerDown) {
            this._pointerDown = false;
            this.releasePointerCapture(event.pointerId);
        }
    }
    /**
     * Handles keyboard navigation.
     *
     * @private
     */
    onKeyDown(event) {
        if (this.disabled) {
            return;
        }
        const step = event.shiftKey ? this._step * 10 : this._step;
        let handled = false;
        switch (event.key) {
            case 'ArrowLeft':
            case 'ArrowDown':
                this.alpha = Math.max(0, this._alpha - step);
                handled = true;
                break;
            case 'ArrowRight':
            case 'ArrowUp':
                this.alpha = Math.min(1, this._alpha + step);
                handled = true;
                break;
            case 'Home':
                this.alpha = 0;
                handled = true;
                break;
            case 'End':
                this.alpha = 1;
                handled = true;
                break;
        }
        if (handled) {
            event.preventDefault();
            this._alphaChanged.emit({ alpha: this._alpha });
        }
    }
    /**
     * Updates the alpha from pointer coordinates.
     *
     * @private
     */
    updateAlphaFromPointer(event) {
        // Always use getBoundingClientRect() for pointer calculations since
        // ResizeObserver's contentRect has left/top = 0 (relative to element, not viewport)
        const rect = this.getBoundingClientRect();
        const x = Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width));
        this.alpha = Math.round(x / this._step) * this._step;
        this._alphaChanged.emit({ alpha: this._alpha });
    }
};
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorAlphaSliderElement.prototype, "alpha", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ColorEditorAlphaSliderElement.prototype, "color", null);
__decorate([
    Property({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorAlphaSliderElement.prototype, "step", null);
__decorate([
    Emit({ eventName: 'alphaChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ColorEditorAlphaSliderElement.prototype, "alphaChanged", null);
ColorEditorAlphaSliderElement = __decorate([
    Component({
        selector: 'mosaik-color-editor-alpha-slider',
        template: colorEditorAlphaSliderElementTemplate,
        themes: {
            joy: colorEditorAlphaSliderElementJoyStyle,
            memphis: colorEditorAlphaSliderElementMemphisStyle,
            cosmopolitan: colorEditorAlphaSliderElementCosmopolitanStyle
        },
        host: {
            'tabIndex': '0',
            'role': 'slider',
            'aria-label': 'Opacity',
            'aria-valuemin': '0',
            'aria-valuemax': '100'
        }
    }),
    __metadata("design:paramtypes", [])
], ColorEditorAlphaSliderElement);
export { ColorEditorAlphaSliderElement };
//# sourceMappingURL=ColorEditorAlphaSliderElement.js.map