// #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);
};
var TableCellElement_1;
import { DomServiceLocator } from '../../../../Dom/Dom';
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { IconElement } from '../../Media/Icon/IconElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { tableCellElementTemplate } from './TableCellElementTemplate';
import { tableCellElementJoyStyle } from './Themes/TableCellElement.Joy';
import { tableCellElementRetroStyle } from './Themes/TableCellElement.Retro';
import { tableCellElementCosmopolitanStyle } from './Themes/TableCellElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { TextFormattable } from '../../../Behaviors/TextFormattable';
import { TextOverflowable } from '../../../Behaviors/TextOverflowable';
// #endregion
/**
 * Table Cell - A single unit within a table's row and column layout.
 *
 * @element mosaik-table-cell
 *
 * @fires tableCellSortChanged - Fired when the sort direction of the cell has changed.
 *
 * @public
 */
let TableCellElement = TableCellElement_1 = class TableCellElement extends Themeable(TextFormattable(TextOverflowable(CustomElement))) {
    // #region Fields
    _sortChanged;
    _key;
    _sorted;
    _sortable;
    _sortDirection;
    _text;
    _clickSubscription;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._key = '';
        this._sorted = false;
        this._sortable = false;
        this._sortDirection = 'none';
        this._text = '';
        this._clickSubscription = null;
        this._sortChanged = new HTMLElementEventEmitter(this, 'tableCellSortChanged');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @readonly
    */
    static get is() {
        return 'mosaik-table-cell';
    }
    /**
     * Gets or sets the `key` property.
     *
     * @public
     * @attr
     */
    get key() {
        return this._key;
    }
    set key(value) {
        if (this._key !== value) {
            this._key = value;
            this.requestUpdate('key');
        }
    }
    /**
     * Gets or sets the `sortable` property.
     *
     * @public
     * @attr
     */
    get sortable() {
        return this._sortable;
    }
    set sortable(value) {
        if (this._sortable !== value) {
            this._sortable = value;
            this.requestUpdate('sortable');
        }
    }
    /**
     * Gets or sets the `text` property.
     *
     * @public
     * @attr
     */
    get text() {
        return this._text;
    }
    set text(value) {
        if (this._text !== value) {
            this._text = value;
            this.requestUpdate('text');
        }
    }
    /**
     * Gets or sets the `sortDirection` property.
     *
     * @public
     * @attr
     */
    get sortDirection() {
        return this._sortDirection;
    }
    set sortDirection(value) {
        if (this._sortDirection !== value) {
            this._sortDirection = value;
            this.requestUpdate('sortDirection');
        }
    }
    /**
     * Called when the sort direction changes.
     * Provides reference to `{@link ITableCellSortChangedEventDetail}` as event argument.
     *
     * @public
     * @readonly
     * @eventProperty
     * @type IEventEmitter<ITableCellSortChangedEventDetail>
     */
    get sortChanged() {
        return this._sortChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    sort(direction) {
        this.sortDirection = direction;
        this._sorted = this.sortDirection !== 'none';
        this.requestUpdate('sortDirection');
        this._sortChanged.emit({
            sortKey: this._key,
            sortDirection: this._sortDirection
        });
    }
    /**
     * @public
     */
    unsort() {
        this.sortDirection = 'none';
        this._sorted = false;
        this.requestUpdate('sortDirection');
        this._sortChanged.emit({
            sortKey: this._key,
            sortDirection: this._sortDirection
        });
    }
    /**
     * @public
     * @override
     */
    attributeChangedCallback(name, oldValue, newValue) {
        super.attributeChangedCallback(name, oldValue, newValue);
        if (name === 'sortable') {
            if (this._sortable) {
                this._clickSubscription = this.on('click', () => this.handleSort());
            }
            else {
                this._clickSubscription?.dispose();
            }
        }
    }
    /**
     * @private
     */
    handleSort() {
        let nextSortDirection = 'none';
        switch (this._sortDirection) {
            case 'asc':
                nextSortDirection = 'desc';
                break;
            case 'desc':
                nextSortDirection = 'none';
                break;
            case 'none':
                nextSortDirection = 'asc';
                break;
            default:
                nextSortDirection = 'none';
                break;
        }
        this.sort(nextSortDirection);
        if (!this._sorted) {
            // unsort other heads otherwise
            const cells = this.parentElement
                ? DomServiceLocator.current.findDescendants(this.parentElement, TableCellElement_1)
                : null;
            cells?.forEach((x) => x.unsort());
            this.sort(this._sortDirection);
        }
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], TableCellElement.prototype, "key", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], TableCellElement.prototype, "sortable", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], TableCellElement.prototype, "text", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], TableCellElement.prototype, "sortDirection", null);
__decorate([
    Emit({ eventName: 'tableCellSortChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], TableCellElement.prototype, "sortChanged", null);
TableCellElement = TableCellElement_1 = __decorate([
    Component({
        selector: 'mosaik-table-cell',
        template: tableCellElementTemplate,
        themes: {
            joy: tableCellElementJoyStyle,
            retro: tableCellElementRetroStyle,
            cosmopolitan: tableCellElementCosmopolitanStyle
        },
        imports: [
            TextElement,
            IconElement
        ]
    }),
    __metadata("design:paramtypes", [])
], TableCellElement);
export { TableCellElement };
//# sourceMappingURL=TableCellElement.js.map