// #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 { colorEditorShadeSliderElementTemplate } from './ColorEditorShadeSliderElementTemplate';
import { colorEditorShadeSliderElementCosmopolitanStyle } from './Themes/ColorEditorShadeSliderElement.Cosmopolitan';
import { colorEditorShadeSliderElementJoyStyle } from './Themes/ColorEditorShadeSliderElement.Joy';
import { colorEditorShadeSliderElementMemphisStyle } from './Themes/ColorEditorShadeSliderElement.Memphis';
// #endregion
/**
 * Color Editor Shade Slider - A slider for selecting shade/darkness values.
 *
 * @name ColorEditorShadeSlider
 * @element mosaik-color-editor-shade-slider
 *
 * @description
 * The Color Editor Shade Slider component provides a horizontal slider displaying
 * a gradient from the current color (at full saturation) to black. Users can drag
 * or click to select a shade value which controls how much black is mixed into the color.
 * A shade of 0 means the full color, while 100 means completely black.
 *
 * @category Media
 *
 * @fires shadeChanged {ShadeChangedEvent} - Fired when the shade value changes via user interaction
 *
 * @csspart thumb - The draggable indicator showing current shade position
 *
 * @public
 */
let ColorEditorShadeSliderElement = class ColorEditorShadeSliderElement extends Themeable(Disableable(Focusable(CustomElement))) {
    // #region Fields
    _resizeController;
    _shadeChanged;
    _hue;
    _saturation;
    _shade;
    _step;
    _boundingClientRect;
    _pointerDown;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the ColorEditorShadeSliderElement class.
     *
     * @public
     */
    constructor() {
        super();
        this._shadeChanged = new HTMLElementEventEmitter(this, 'shadeChanged');
        this._hue = 0;
        this._saturation = 100;
        this._shade = 0;
        this._step = 1;
        this._pointerDown = false;
        this._resizeController = new ResizeController(this, {
            callback: (entries) => {
                for (const entry of entries) {
                    this._boundingClientRect = entry.contentRect;
                }
            }
        });
    }
    // #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-editor-shade-slider';
    }
    /**
     * Gets or sets the hue value (0-360) for the gradient color.
     *
     * @public
     * @attr
     */
    get hue() {
        return this._hue;
    }
    set hue(value) {
        if (this._hue !== value) {
            this._hue = Math.max(0, Math.min(360, value));
            this.requestUpdate('hue');
        }
    }
    /**
     * Gets or sets the saturation value (0-100) for the gradient color.
     *
     * @public
     * @attr
     */
    get saturation() {
        return this._saturation;
    }
    set saturation(value) {
        if (this._saturation !== value) {
            this._saturation = Math.max(0, Math.min(100, value));
            this.requestUpdate('saturation');
        }
    }
    /**
     * Gets or sets the shade value (0-100).
     * 0 = full color, 100 = completely black.
     *
     * @public
     * @attr
     */
    get shade() {
        return this._shade;
    }
    set shade(value) {
        if (this._shade !== value) {
            this._shade = Math.max(0, Math.min(100, value));
            this.requestUpdate('shade');
        }
    }
    /**
     * 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.1, value);
            this.requestUpdate('step');
        }
    }
    /**
     * Called when the shade value changes.
     * Provides reference to `{@link IShadeChangedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get shadeChanged() {
        return this._shadeChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @inheritdoc
     * @protected
     */
    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', this._shade.toString());
    }
    /**
     * Handles pointer down events to start dragging.
     *
     * @private
     */
    onPointerDown(event) {
        if (this.disabled) {
            return;
        }
        this._pointerDown = true;
        this.setPointerCapture(event.pointerId);
        this.updateShadeFromPointer(event);
    }
    /**
     * Handles pointer move events for dragging.
     *
     * @private
     */
    onPointerMove(event) {
        if (!this._pointerDown || this.disabled) {
            return;
        }
        this.updateShadeFromPointer(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.shade = Math.max(0, this._shade - step);
                handled = true;
                break;
            case 'ArrowRight':
            case 'ArrowUp':
                this.shade = Math.min(100, this._shade + step);
                handled = true;
                break;
            case 'Home':
                this.shade = 0;
                handled = true;
                break;
            case 'End':
                this.shade = 100;
                handled = true;
                break;
        }
        if (handled) {
            event.preventDefault();
            this._shadeChanged.emit({ shade: this._shade });
        }
    }
    /**
     * Updates the shade from pointer coordinates.
     *
     * @private
     */
    updateShadeFromPointer(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.shade = Math.round(x * 100 / this._step) * this._step;
        this._shadeChanged.emit({ shade: this._shade });
    }
};
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorShadeSliderElement.prototype, "hue", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorShadeSliderElement.prototype, "saturation", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorShadeSliderElement.prototype, "shade", null);
__decorate([
    Property({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ColorEditorShadeSliderElement.prototype, "step", null);
__decorate([
    Emit({ eventName: 'shadeChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ColorEditorShadeSliderElement.prototype, "shadeChanged", null);
ColorEditorShadeSliderElement = __decorate([
    Component({
        selector: 'mosaik-color-editor-shade-slider',
        template: colorEditorShadeSliderElementTemplate,
        themes: {
            joy: colorEditorShadeSliderElementJoyStyle,
            memphis: colorEditorShadeSliderElementMemphisStyle,
            cosmopolitan: colorEditorShadeSliderElementCosmopolitanStyle
        },
        host: {
            'tabIndex': '0',
            'role': 'slider',
            'aria-label': 'Shade',
            'aria-valuemin': '0',
            'aria-valuemax': '100'
        }
    }),
    __metadata("design:paramtypes", [])
], ColorEditorShadeSliderElement);
export { ColorEditorShadeSliderElement };
//# sourceMappingURL=ColorEditorShadeSliderElement.js.map