/* eslint-disable @typescript-eslint/naming-convention */
// #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 { Listen } from '../../Events/Decorators/ListenDecorator';
import { Attribute } from '../Decorators/AttributeDecorator';
/**
 * @fires checked {CustomEvent<IEventDetail>} - Fired when the `isChecked` property is `true`.
 * @fires unchecked {CustomEvent<IEventDetail>} - Fired when the `isChecked` property is `false`.
 *
 * @public
 */
export const Checkable = (base) => {
    class CheckableElement extends base {
        // #region Fields
        _checked;
        _unchecked;
        _isChecked;
        // #endregion
        // #region Ctor
        /**
         * Constructs a new instance of the `CheckableElement` class.
         *
         * @public
         */
        constructor(...args) {
            super(args);
            this._isChecked = false;
            this._checked = new HTMLElementEventEmitter(this, 'checked');
            this._unchecked = new HTMLElementEventEmitter(this, 'unchecked');
        }
        // #endregion
        // #region Properties
        /**
         * Gets or sets the `isChecked` property.
         * The default value is `false`, which means the element is not checked.
         *
         * @public
         * @attr
         */
        get isChecked() {
            return this._isChecked;
        }
        set isChecked(value) {
            if (this.disabled) {
                return;
            }
            if (this._isChecked !== value) {
                this.onIsCheckedChanged(this._isChecked, value);
            }
        }
        /**
         * Called when the `isChecked` property is `true`.
         * Provides reference to the `IEventDetail` as event argument.
         *
         * @public
         * @eventProperty
         * @readonly
         */
        get checked() {
            return this._checked;
        }
        /**
         * Called when the `isChecked` property is `false`.
         * Provides reference to the `IEventDetail` as event argument.
         *
         * @public
         * @eventProperty
         * @readonly
         */
        get unchecked() {
            return this._checked;
        }
        // #endregion
        // #region Methods
        /**
         * @public
         */
        check() {
            if (this.disabled) {
                return;
            }
            if (this._isChecked) {
                return;
            }
            this.isChecked = true;
        }
        /**
         * @public
         */
        uncheck() {
            if (this.disabled) {
                return;
            }
            if (!this._isChecked) {
                return;
            }
            this.isChecked = false;
        }
        /**
         * @public
         */
        toggle() {
            if (this.disabled) {
                return;
            }
            this.isChecked = !this.isChecked;
        }
        /**
         * This method is invoked when the `isChecked` property is changed.
         *
         * @protected
         * @virtual
         */
        onIsCheckedChanged(_prev, newValue) {
            this._isChecked = newValue;
            this.requestUpdate('isChecked');
            if (newValue) {
                this._checked.emit();
            }
            else {
                this._unchecked.emit();
            }
        }
        /**
         * @protected
         */
        handleClick() {
            if (this.disabled) {
                return;
            }
            this.toggle();
        }
    }
    __decorate([
        Attribute({
            type: Boolean,
            useDefault: true
        }),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [Boolean])
    ], CheckableElement.prototype, "isChecked", null);
    __decorate([
        Emit(),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], CheckableElement.prototype, "checked", null);
    __decorate([
        Emit(),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], CheckableElement.prototype, "unchecked", null);
    __decorate([
        Listen('click'),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], CheckableElement.prototype, "handleClick", null);
    return CheckableElement;
};
//# sourceMappingURL=Checkable.js.map