// #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 { RendererServiceLocator } from '../../../../Dom/Renderer';
import { Themeable } from '../../../Behaviors/Themeable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { gridItemElementTemplate } from './GridItemElementTemplate';
import { gridItemElementCosmopolitanStyle } from './Themes/GridItemElement.Cosmopolitan';
import { gridItemElementJoyStyle } from './Themes/GridItemElement.Joy';
import { gridItemElementMemphisStyle } from './Themes/GridItemElement.Memphis';
// #endregion
/**
 * Grid Item - An element within a grid layout, organized in rows and columns.
 *
 * @description
 * The Grid Item component is placed inside a `GridElement`
 * and defines its position and span within the grid via the `column`, `row`, `column-span`, and `row-span` attributes.
 *
 * Positions are 1-based. A value of `0` means auto-placement.
 * Positions and spans that exceed the parent grid's explicit track
 * definitions are clamped to `auto` to prevent implicit track creation.
 *
 * @name Grid Item
 * @element mosaik-grid-item
 * @category Layout
 *
 * @slot - The default slot.
 *
 * @cssprop {String} --grid-item-font-family - The item font family CSS custom property.
 * @cssprop {String} --grid-item-font-letter-spacing - The item font letter spacing CSS custom property.
 * @cssprop {String} --grid-item-font-line-height - The item font line height CSS custom property.
 * @cssprop {String} --grid-item-font-size - The item font size CSS custom property.
 * @cssprop {String} --grid-item-font-text-decoration - The item font text decoration CSS custom property.
 * @cssprop {String} --grid-item-font-text-transform - The item font text transform CSS custom property.
 * @cssprop {String} --grid-item-font-weight - The item font weight CSS custom property.
 * @cssprop {String} --grid-item-gap - The item gap CSS custom property.
 * @cssprop {String} --grid-item-padding-bottom - The item padding bottom CSS custom property.
 * @cssprop {String} --grid-item-padding-left - The item padding left CSS custom property.
 * @cssprop {String} --grid-item-padding-right - The item padding right CSS custom property.
 * @cssprop {String} --grid-item-padding-top - The item padding top CSS custom property.
 * @cssprop {String} --grid-item-shadow - The item shadow CSS custom property.
 * @cssprop {String} --grid-item-shadow-blur - The item shadow blur CSS custom property.
 * @cssprop {String} --grid-item-shadow-color - The item shadow color CSS custom property.
 * @cssprop {String} --grid-item-shadow-offset-x - The item shadow offset x CSS custom property.
 * @cssprop {String} --grid-item-shadow-offset-y - The item shadow offset y CSS custom property.
 * @cssprop {String} --grid-item-shadow-spread - The item shadow spread CSS custom property.
 * @cssprop {String} --grid-item-transition-duration - The item transition duration CSS custom property.
 * @cssprop {String} --grid-item-transition-mode - The item transition mode CSS custom property.
 * @cssprop {String} --grid-item-transition-property - The item transition property CSS custom property.
 * @cssprop {String} --grid-item-translate - The item translate CSS custom property.
 *
 * @example
 * Item spanning two columns in a content layout:
 * ```html
 * <mosaik-grid column-definitions="* * *" row-definitions="auto auto" gap="16">
 *   <mosaik-grid-item column="1" row="1" column-span="2">Wide Content</mosaik-grid-item>
 *   <mosaik-grid-item column="3" row="1">Aside</mosaik-grid-item>
 *   <mosaik-grid-item column="1" row="2" column-span="3">Full-Width Footer</mosaik-grid-item>
 * </mosaik-grid>
 * ```
 *
 * @public
 */
let GridItemElement = class GridItemElement extends Themeable(CustomElement) {
    // #region Fields
    _column;
    _columnSpan;
    _row;
    _rowSpan;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `GridItemElement` class.
     *
     * @public
     */
    constructor() {
        super();
        this._column = 0;
        this._columnSpan = 0;
        this._row = 0;
        this._rowSpan = 0;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @readonly
    */
    static get is() {
        return 'mosaik-grid-item';
    }
    /**
     * Gets or sets the `column` property.
     *
     * @public
     * @attr column
     */
    get column() {
        return this._column;
    }
    set column(value) {
        this._column = value;
    }
    /**
     * Gets or sets the `columnSpan` property.
     *
     * @public
     * @attr column-span
     */
    get columnSpan() {
        return this._columnSpan;
    }
    set columnSpan(value) {
        this._columnSpan = value;
    }
    /**
     * Gets or sets the `row` property.
     *
     * @public
     * @attr row
     */
    get row() {
        return this._row;
    }
    set row(value) {
        this._row = value;
    }
    /**
     * Gets or sets the `rowSpan` property.
     *
     * @public
     * @attr row-span
     */
    get rowSpan() {
        return this._rowSpan;
    }
    set rowSpan(value) {
        this._rowSpan = value;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        this.setItemStyle();
    }
    /**
     * Re-evaluates and applies the CSS `grid-area` placement of this item.
     *
     * Called by the parent `GridElement` when `columnDefinitions` or
     * `rowDefinitions` change so that out-of-bounds positions are clamped
     * to `auto`, preventing CSS Grid from creating implicit tracks.
     *
     * @public
     */
    invalidatePosition() {
        this.setItemStyle();
    }
    /**
     * @protected
     */
    onRowPropertyChanged() {
        this.setItemStyle();
    }
    /**
     * @protected
     */
    onColumnPropertyChanged() {
        this.setItemStyle();
    }
    /**
     * @protected
     */
    onRowSpanPropertyChanged() {
        this.setItemStyle();
    }
    /**
     * @protected
     */
    onColumnSpanPropertyChanged() {
        this.setItemStyle();
    }
    /**
     * Computes and applies the CSS `grid-area` style based on the configured
     * row/column positions and spans, clamped against the parent grid's
     * explicit track definitions.
     *
     * @private
     */
    setItemStyle() {
        const maxColumns = this.getParentTrackCount('columnDefinitions');
        const maxRows = this.getParentTrackCount('rowDefinitions');
        const row = this.resolvePosition(this._row, maxRows);
        const column = this.resolvePosition(this._column, maxColumns);
        const rowSpan = this.resolveSpan(this._rowSpan, row, maxRows);
        const columnSpan = this.resolveSpan(this._columnSpan, column, maxColumns);
        const gridAreaStyle = `${row} / ${column} / ${rowSpan} / ${columnSpan}`;
        RendererServiceLocator.current.setStyle(this, 'grid-area', gridAreaStyle);
    }
    /**
     * Resolves a 1-based track position against the available track count.
     * Returns `'auto'` when the position is unset (`0`) or exceeds the
     * explicit grid tracks defined by the parent.
     *
     * @param position - The 1-based track position (`0` = unset).
     * @param maxTracks - Number of explicitly defined tracks (`0` = unconstrained).
     * @returns The position value or `'auto'`.
     *
     * @private
     */
    resolvePosition(position, maxTracks) {
        if (position <= 0) {
            return 'auto';
        }
        if (maxTracks > 0 && position > maxTracks) {
            return 'auto';
        }
        return position;
    }
    /**
     * Resolves a track span against the available grid space.
     * When the resolved position is `'auto'` (out of bounds or unset), the span is
     * clamped to `maxTracks` to prevent implicit track creation.
     * When the position is in bounds, the span is clamped so the item does not
     * extend beyond the explicit grid.
     *
     * @param span - Requested span count (`0` = no span).
     * @param resolvedPosition - Already-resolved position (`number` or `'auto'`).
     * @param maxTracks - Number of explicitly defined tracks (`0` = unconstrained).
     * @returns A CSS span value string (e.g. `'span 2'`) or `'auto'`.
     *
     * @private
     */
    resolveSpan(span, resolvedPosition, maxTracks) {
        if (span <= 0) {
            return 'auto';
        }
        // No explicit tracks defined - no constraint
        if (maxTracks === 0) {
            return `span ${span}`;
        }
        // Position was clamped to auto - clamp span to total available tracks
        if (resolvedPosition === 'auto') {
            const clamped = Math.min(span, maxTracks);
            return clamped > 0 ? `span ${clamped}` : 'auto';
        }
        // Position is in bounds - clamp span so it does not exceed remaining tracks
        if (typeof resolvedPosition === 'number') {
            const remaining = maxTracks - resolvedPosition + 1;
            const clamped = Math.min(span, remaining);
            return clamped > 0 ? `span ${clamped}` : 'auto';
        }
        return `span ${span}`;
    }
    /**
     * Reads the number of explicitly defined tracks from the parent grid element.
     * Uses duck-typing (`property in parent`) so that `GridItemElement` does not
     * import `GridElement`, avoiding any risk of circular module dependencies.
     *
     * @param property - The parent property name (`'columnDefinitions'` or `'rowDefinitions'`).
     * @returns The number of explicit track definitions, or `0` when unconstrained.
     *
     * @private
     */
    getParentTrackCount(property) {
        const parent = this.parentElement;
        if (parent != null && property in parent) {
            const value = parent[property];
            if (Array.isArray(value)) {
                return value.length;
            }
        }
        return 0;
    }
};
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], GridItemElement.prototype, "column", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], GridItemElement.prototype, "columnSpan", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], GridItemElement.prototype, "row", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], GridItemElement.prototype, "rowSpan", null);
__decorate([
    Watch('row'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], GridItemElement.prototype, "onRowPropertyChanged", null);
__decorate([
    Watch('column'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], GridItemElement.prototype, "onColumnPropertyChanged", null);
__decorate([
    Watch('rowSpan'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], GridItemElement.prototype, "onRowSpanPropertyChanged", null);
__decorate([
    Watch('columnSpan'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], GridItemElement.prototype, "onColumnSpanPropertyChanged", null);
GridItemElement = __decorate([
    Component({
        selector: 'mosaik-grid-item',
        themes: {
            joy: gridItemElementJoyStyle,
            memphis: gridItemElementMemphisStyle,
            cosmopolitan: gridItemElementCosmopolitanStyle
        },
        template: gridItemElementTemplate
    }),
    __metadata("design:paramtypes", [])
], GridItemElement);
export { GridItemElement };
//# sourceMappingURL=GridItemElement.js.map