// #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 { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { Busyable } from '../../../Behaviors/Busyable';
import { ContentAlignable } from '../../../Behaviors/ContentAlignable';
import { Fitable } from '../../../Behaviors/Fitable';
import { Labelable } from '../../../Behaviors/Labelable';
import { Orientable } from '../../../Behaviors/Orientable';
import { Reversible } from '../../../Behaviors/Reversible';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { RippleElement } from '../../Effects/Ripple/RippleElement';
import { StackElement } from '../../Layouts/Stack/StackElement';
import { IconElement } from '../../Media/Icon/IconElement';
import { FocusRingElement } from '../../Primitives/FocusRing/FocusRingElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { ProgressRingElement } from '../../Ranges/ProgressRing/ProgressRingElement';
import { ButtonBaseElement } from '../Abstracts/ButtonBaseElement';
import { repeatButtonElementTemplate } from './RepeatButtonElementTemplate';
import { repeatButtonElementJoyStyle } from './Themes/RepeatButtonElement.Joy';
import { repeatButtonElementRetroStyle } from './Themes/RepeatButtonElement.Retro';
import { repeatButtonElementCosmopolitanStyle } from './Themes/RepeatButtonElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { HTMLElementEventEmitter, on } from '@breadstone/mosaik-elements';
import { Iconable } from '../../../Behaviors/Iconable';
// #endregion
/**
 * Repeat Button - An interactive button element designed to trigger an action repeatedly when held down.
 *
 * @description
 * The Repeat Button is an interactive UI element that allows users to trigger an action repeatedly by holding down
 * the button. This component is often used for actions that require continuous input, such as adjusting a value or
 * repeatedly executing a command. It provides a user-friendly way to perform actions in a continuous manner.
 *
 * @name Repeat Button
 * @element mosaik-repeat-button
 * @category Buttons
 *
 * @slot label - The label slot for text or labels inside the button.
 * @slot icon - The icon slot for placing an icon inside the button.
 * @slot overlay - The overlay slot (useful for badges or additional visual elements).
 *
 * @csspart button - The button part of the Repeat Button.
 * @csspart focusRing - The focus ring part of the Repeat Button.
 * @csspart ripple - The ripple effect part of the Repeat Button.
 * @csspart icon - The icon part of the Repeat Button.
 * @csspart label - The label part of the Repeat Button.
 * @csspart progressRing - The progress ring part of the Repeat Button.
 *
 * @cssprop {String} --repeat-button-font-family - The font family of the Repeat Button.
 * @cssprop {String} --repeat-button-font-size - The font size of the Repeat Button.
 * @cssprop {String} --repeat-button-font-line-height - The font line height of the Repeat Button.
 * @cssprop {String} --repeat-button-font-weight - The font weight of the Repeat Button.
 * @cssprop {String} --repeat-button-font-letter-spacing - The font letter spacing of the Repeat Button.
 * @cssprop {String} --repeat-button-font-text-decoration - The font text decoration of the Repeat Button.
 * @cssprop {String} --repeat-button-font-text-transform - The font text transform of the Repeat Button.
 * @cssprop {String} --repeat-button-padding-top - The padding top of the Repeat Button.
 * @cssprop {String} --repeat-button-padding-right - The padding right of the Repeat Button.
 * @cssprop {String} --repeat-button-padding-bottom - The padding bottom of the Repeat Button.
 * @cssprop {String} --repeat-button-padding-left - The padding left of the Repeat Button.
 * @cssprop {String} --repeat-button-gap - The gap between elements of the Repeat Button.
 * @cssprop {String} --repeat-button-transition-duration - The transition duration for the Repeat Button.
 * @cssprop {String} --repeat-button-transition-mode - The transition mode for the Repeat Button.
 * @cssprop {String} --repeat-button-transition-property - The transition property for the Repeat Button.
 * @cssprop {String} --repeat-button-background-color - The background color of the Repeat Button.
 * @cssprop {String} --repeat-button-border-color - The border color of the Repeat Button.
 * @cssprop {String} --repeat-button-foreground-color - The foreground color of the Repeat Button.
 * @cssprop {String} --repeat-button-border-width - The border width of the Repeat Button.
 * @cssprop {String} --repeat-button-border-radius - The border radius of the Repeat Button.
 * @cssprop {String} --repeat-button-border-style - The border style of the Repeat Button.
 * @cssprop {String} --repeat-button-shadow - The shadow of the Repeat Button.
 * @cssprop {String} --focus-ring-outward-offset - The outward offset of the focus ring.
 * @cssprop {String} --repeat-button-line-height - The line height of the Repeat Button text.
 *
 * @fires repeated - Called when the button is repeated.
 *
 * @dependency {StackElement} mosaik-stack - The Stack component, used for layout management.
 * @dependency {IconElement} mosaik-icon - The Icon component, used for displaying the icon.
 * @dependency {RippleElement} mosaik-ripple - The Ripple component, used for the ripple effect.
 * @dependency {FocusRingElement} mosaik-focus-ring - The Focus Ring component, used for focus indication.
 * @dependency {TextElement} mosaik-text - The Text component, used for displaying the label text.
 * @dependency {ProgressRingElement} mosaik-progress-ring - The Progress Ring component, used for progress indication.
 *
 * @example
 * ```html
 * <mosaik-repeat-button>
 *   <mosaik-icon name="repeat"></mosaik-icon>
 *   <span>Repeat</span>
 * </mosaik-repeat-button>
 * ```
 *
 * @public
 */
let RepeatButtonElement = class RepeatButtonElement extends Themeable(Reversible(Orientable(ContentAlignable(Fitable(Busyable(Labelable(Iconable(ButtonBaseElement)))))))) {
    // #region Fields
    _delay;
    _interval;
    _isMouseKeydown;
    _delayHandler;
    _intervalHandler;
    _isRepeating;
    _clickEventSubscription;
    _mouseUpEventSubscription;
    _mouseLeaveEventSubscription;
    _repeated;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._delay = 500;
        this._interval = 330;
        this._isMouseKeydown = false;
        this._delayHandler = null;
        this._intervalHandler = null;
        this._isRepeating = false;
        this._clickEventSubscription = null;
        this._mouseUpEventSubscription = null;
        this._mouseLeaveEventSubscription = null;
        this._repeated = new HTMLElementEventEmitter(this, 'repeated');
    }
    // #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-repeat-button';
    }
    /**
     * Gets or sets the `delay` property.
     *
     * @public
     * @attr
     */
    get delay() {
        return this._delay;
    }
    set delay(value) {
        if (this._delay !== value) {
            this._delay = value;
            this.requestUpdate('delay');
        }
    }
    /**
     * Gets or sets the `interval` property.
     *
     * @public
     * @attr
     */
    get interval() {
        return this._interval;
    }
    set interval(value) {
        if (this._interval !== value) {
            this._interval = value;
            this.requestUpdate('interval');
        }
    }
    /**
     * Gets or sets the `isRepeating` property.
     *
     * @public
     * @readonly
     * @attr
     */
    get isRepeating() {
        return this._isRepeating;
    }
    /**
     * @private
     */
    set isRepeating(value) {
        if (this._isRepeating !== value) {
            this._isRepeating = value;
            this.requestUpdate('isRepeating');
        }
    }
    /**
     * Called when the button is repeated.
     * Provides reference to `{@link IRepeatButtonRepeatedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get repeated() {
        return this._repeated;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        super.disconnectedCallback();
        this._clickEventSubscription?.dispose();
        this._mouseUpEventSubscription?.dispose();
        this._mouseLeaveEventSubscription?.dispose();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        const buttonElement = this.getTemplatePart('button');
        this._clickEventSubscription = on(buttonElement, 'click', (event) => this.onClick(event));
        this._mouseUpEventSubscription = on(buttonElement, 'mouseup', (event) => this.onMouseUp(event));
        this._mouseLeaveEventSubscription = on(buttonElement, 'mouseleave', (event) => this.onMouseLeave(event));
    }
    /**
     * Emits the {@link repeated} event.
     *
     * @protected
     */
    onRepeated(args) {
        this._repeated.emit(args);
    }
    /**
     * @private
     */
    onClick(event) {
        if (event.button !== 0 || this._isMouseKeydown) {
            return;
        }
        this._isMouseKeydown = true;
        let repeatCount = 0;
        this._delayHandler = setTimeout(() => {
            if (!this._isMouseKeydown || this._intervalHandler !== null) {
                return;
            }
            this._intervalHandler = setInterval(() => {
                this.isRepeating = true;
                // Increment repeat count on each interval
                repeatCount++;
                this.onRepeated({
                    repeatCount: repeatCount
                });
            }, this.interval);
        }, this.delay);
    }
    /**
     * @private
     */
    onMouseUp(event) {
        event.preventDefault();
        this.release();
    }
    /**
     * @private
     */
    onMouseLeave(event) {
        event.preventDefault();
        this.release();
    }
    /**
     * @private
     */
    release() {
        this._isMouseKeydown = false;
        clearTimeout(this._delayHandler ?? undefined);
        clearInterval(this._intervalHandler ?? undefined);
        this._delayHandler = null;
        this._intervalHandler = null;
        this.isRepeating = false;
    }
};
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], RepeatButtonElement.prototype, "delay", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], RepeatButtonElement.prototype, "interval", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], RepeatButtonElement.prototype, "isRepeating", null);
__decorate([
    Emit({ eventName: 'repeated' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], RepeatButtonElement.prototype, "repeated", null);
RepeatButtonElement = __decorate([
    Component({
        selector: 'mosaik-repeat-button',
        template: repeatButtonElementTemplate,
        themes: {
            joy: repeatButtonElementJoyStyle,
            retro: repeatButtonElementRetroStyle,
            cosmopolitan: repeatButtonElementCosmopolitanStyle
        },
        options: {
            mode: 'open',
            delegatesFocus: true
        },
        imports: [
            StackElement,
            IconElement,
            RippleElement,
            FocusRingElement,
            TextElement,
            ProgressRingElement
        ]
    }),
    __metadata("design:paramtypes", [])
], RepeatButtonElement);
export { RepeatButtonElement };
//# sourceMappingURL=RepeatButtonElement.js.map