// #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 { DomServiceLocator } from '../../../../Dom/Dom';
import { Selectable } from '../../../Behaviors/Selectable';
import { Themeable } from '../../../Behaviors/Themeable';
import { Valueable } from '../../../Behaviors/Valueable';
import { addInheritanceConsumerController } from '../../../Controllers/InheritanceConsumerController';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { TableSelectionMode } from '../../../Types/TableSelectionMode';
import { TableSelectionType } from '../../../Types/TableSelectionType';
import { TextElement } from '../../Primitives/Text/TextElement';
import { TableCellBaseElement } from './Abstracts2/TableCellBaseElement';
import { tableBodyCellElementTemplate } from './TableBodyCellElementTemplate';
import { TableBodyElement } from './TableBodyElement';
import { TableElement } from './TableElement';
import { TABLE_INHERITANCE_CONTEXT } from './TableInheritanceContext';
import { tableBodyCellElementCosmopolitanStyle } from './Themes/TableBodyCellElement.Cosmopolitan';
import { tableBodyCellElementJoyStyle } from './Themes/TableBodyCellElement.Joy';
import { tableBodyCellElementMemphisStyle } from './Themes/TableBodyCellElement.Memphis';
// #endregion
/**
 * Table Body Cell - A selectable cell within a table body row.
 *
 * @element mosaik-table-body-cell
 *
 * @slot - The default slot.
 *
 * @csspart text - The text part.
 *
 * @cssprop {String} --table-body-cell-background-color - The cell background color CSS custom property.
 * @cssprop {String} --table-body-cell-border-color - The cell border color CSS custom property.
 * @cssprop {String} --table-body-cell-border-radius - The cell border radius CSS custom property.
 * @cssprop {String} --table-body-cell-border-width - The cell border width CSS custom property.
 * @cssprop {String} --table-body-cell-font-family - The cell font family CSS custom property.
 * @cssprop {String} --table-body-cell-font-letter-spacing - The cell font letter spacing CSS custom property.
 * @cssprop {String} --table-body-cell-font-line-height - The cell font line height CSS custom property.
 * @cssprop {String} --table-body-cell-font-size - The cell font size CSS custom property.
 * @cssprop {String} --table-body-cell-font-text-decoration - The cell font text decoration CSS custom property.
 * @cssprop {String} --table-body-cell-font-text-transform - The cell font text transform CSS custom property.
 * @cssprop {String} --table-body-cell-font-weight - The cell font weight CSS custom property.
 * @cssprop {String} --table-body-cell-foreground-color - The cell foreground color CSS custom property.
 * @cssprop {String} --table-body-cell-gap - The cell gap CSS custom property.
 * @cssprop {String} --table-body-cell-padding-bottom - The cell padding bottom CSS custom property.
 * @cssprop {String} --table-body-cell-padding-left - The cell padding left CSS custom property.
 * @cssprop {String} --table-body-cell-padding-right - The cell padding right CSS custom property.
 * @cssprop {String} --table-body-cell-padding-top - The cell padding top CSS custom property.
 * @cssprop {String} --table-body-cell-shadow - The cell shadow CSS custom property.
 * @cssprop {String} --table-body-cell-shadow-blur - The cell shadow blur CSS custom property.
 * @cssprop {String} --table-body-cell-shadow-color - The cell shadow color CSS custom property.
 * @cssprop {String} --table-body-cell-shadow-offset-x - The cell shadow offset x CSS custom property.
 * @cssprop {String} --table-body-cell-shadow-offset-y - The cell shadow offset y CSS custom property.
 * @cssprop {String} --table-body-cell-shadow-spread - The cell shadow spread CSS custom property.
 * @cssprop {String} --table-body-cell-transition-duration - The cell transition duration CSS custom property.
 * @cssprop {String} --table-body-cell-transition-mode - The cell transition mode CSS custom property.
 * @cssprop {String} --table-body-cell-transition-property - The cell transition property CSS custom property.
 * @cssprop {String} --table-body-cell-translate - The cell translate CSS custom property.
 *
 * @fires selected {SelectedEvent} - Fired when the cell is selected.
 * @fires deselected {DeselectedEvent} - Fired when the cell is deselected.
 *
 * @dependency mosaik-text - The Text element.
 *
 * @example
 * Selectable body cell:
 * ```html
 * <mosaik-table selection-mode="single" selection-type="cell">
 *   <mosaik-table-body>
 *     <mosaik-table-body-row>
 *       <mosaik-table-body-cell text="Alice"></mosaik-table-body-cell>
 *     </mosaik-table-body-row>
 *   </mosaik-table-body>
 * </mosaik-table>
 * ```
 *
 * @public
 */
let TableBodyCellElement = class TableBodyCellElement extends Themeable(Valueable(Selectable(TableCellBaseElement))) {
    // #region Fields
    _inheritance = addInheritanceConsumerController(this, {
        context: TABLE_INHERITANCE_CONTEXT
    });
    _selectionClickSubscription;
    _table;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._selectionClickSubscription = null;
        this._table = null;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-table-body-cell';
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        super.disconnectedCallback();
        this._selectionClickSubscription?.dispose();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        const table = DomServiceLocator.current.findAncestor(this, TableElement);
        if (table) {
            this._table = table;
            this.updateSelectionClickHandler();
        }
    }
    /**
     * @protected
     */
    onSelectionModePropertyChanged(_prev, _next) {
        this.updateSelectionClickHandler();
    }
    /**
     * @protected
     */
    onSelectionTypePropertyChanged(_prev, _next) {
        this.updateSelectionClickHandler();
    }
    /**
     * Sets up or tears down the click handler based on the inherited selection config.
     *
     * @private
     */
    updateSelectionClickHandler() {
        this._selectionClickSubscription?.dispose();
        this._selectionClickSubscription = null;
        const selectionMode = this._inheritance.get('selectionMode', TableSelectionMode.None);
        const selectionType = this._inheritance.get('selectionType', TableSelectionType.Row);
        if (selectionMode !== TableSelectionMode.None && selectionType === TableSelectionType.Cell) {
            const isInBody = DomServiceLocator.current.findAncestor(this, TableBodyElement) !== null;
            if (isInBody && this._table) {
                this._selectionClickSubscription = this.on('click', () => this.handleCellClick());
            }
        }
    }
    /**
     * Handles click events for cell selection.
     *
     * @private
     */
    handleCellClick() {
        if (this.disabled || !this._table) {
            return;
        }
        this._table.handleCellSelect(this);
    }
    /**
     * Returns the natural content width of this body cell,
     * measured from inner elements to account for truncated or overflowing text.
     *
     * @public
     */
    getContentWidth() {
        const cs = getComputedStyle(this);
        const padding = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
        // Check slotted content first
        const slot = this.renderRoot?.querySelector('slot');
        if (slot) {
            const elements = slot.assignedElements();
            if (elements.length > 0) {
                let maxWidth = 0;
                for (const element of elements) {
                    maxWidth = Math.max(maxWidth, element.scrollWidth);
                }
                return Math.ceil(maxWidth + padding);
            }
        }
        // Default content (mosaik-text)
        const text = this.renderRoot?.querySelector('[part="text"]');
        if (text) {
            return Math.ceil(text.scrollWidth + padding);
        }
        return this.scrollWidth;
    }
};
__decorate([
    Watch('selectionMode', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TableBodyCellElement.prototype, "onSelectionModePropertyChanged", null);
__decorate([
    Watch('selectionType', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TableBodyCellElement.prototype, "onSelectionTypePropertyChanged", null);
TableBodyCellElement = __decorate([
    Component({
        selector: 'mosaik-table-body-cell',
        template: tableBodyCellElementTemplate,
        themes: {
            joy: tableBodyCellElementJoyStyle,
            memphis: tableBodyCellElementMemphisStyle,
            cosmopolitan: tableBodyCellElementCosmopolitanStyle
        },
        imports: [
            TextElement
        ]
    }),
    __metadata("design:paramtypes", [])
], TableBodyCellElement);
export { TableBodyCellElement };
//# sourceMappingURL=TableBodyCellElement.js.map