// #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 { Themeable } from '../../../Behaviors/Themeable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { HorizontalAlignment } from '../../../Types/HorizontalAlignment';
import { VerticalAlignment } from '../../../Types/VerticalAlignment';
import { LayoutElement } from '../Abstracts/LayoutElement';
import { ColumnDefinitions } from './ColumnDefinitions';
import { gridElementTemplate } from './GridElementTemplate';
import { RowDefinitions } from './RowDefinitions';
import { gridElementJoyStyle } from './Themes/GridElement.Joy';
import { gridElementRetroStyle } from './Themes/GridElement.Retro';
import { gridElementCosmopolitanStyle } from './Themes/GridElement.Cosmopolitan';
// #endregion
/**
 * Grid - A layout system that arranges elements in a structured grid pattern.
 *
 * @element mosaik-grid
 *
 * @slot - The default slot.
 *
 * @public
 */
let GridElement = class GridElement extends Themeable(LayoutElement) {
    // #region Fields
    _rowDefinitions;
    _columnDefinitions;
    _gap;
    _horizontalAlignment;
    _verticalAlignment;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `GridElement` class.
     *
     * @public
     */
    constructor() {
        super();
        this._rowDefinitions = [];
        this._columnDefinitions = [];
        this._gap = 0;
        this._horizontalAlignment = HorizontalAlignment.Stretch;
        this._verticalAlignment = VerticalAlignment.Stretch;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @override
     * @readonly
    */
    static get is() {
        return 'mosaik-grid';
    }
    /**
     * Gets or sets the `rowDefinitions` property.
     * Specifies the number of rows and their height
     *
     * Accepts an array, which serves two purposes:
     *
     * The number of elements in the array defines the number of rows.
     * Each array element defines the size of the corresponding row. The possible array values are:
     * * `number` - Defines the size in pixels.
     * * `string` - Enables the usage of arbitrary units e.g. `20%` or `auto`.
     * * `IRowDefinition` - Configuration object, which accepts a `height` key.
     *
     * @public
     */
    get rowDefinitions() {
        return this._rowDefinitions;
    }
    set rowDefinitions(value) {
        if (this._rowDefinitions !== value) {
            this._rowDefinitions = value;
            this.style.gridTemplateRows = this.generateRowStyle(this._rowDefinitions);
            this.requestUpdate('rowDefinitions');
        }
    }
    /**
     * Gets or sets the `columnDefinitions` property.
     * Specifies the number of columns and their widths
     *
     * Accepts an array, which serves two purposes:
     *
     * The number of elements in the array defines the number of columns.
     * Each array element defines the size of the corresponding column. The possible array values are:
     * * `number` - Defines the size in pixels.
     * * `string` - Enables the usage of arbitrary units e.g. `20%` or `auto`.
     * * `IColumnDefinition` - Configuration object, which accepts a `width` key.
     *
     * @public
     */
    get columnDefinitions() {
        return this._columnDefinitions;
    }
    set columnDefinitions(value) {
        if (this._columnDefinitions !== value) {
            this._columnDefinitions = value;
            this.style.gridTemplateColumns = this.generateColumnStyle(this._columnDefinitions);
            this.requestUpdate('columnDefinitions');
        }
    }
    /**
     * Gets or sets the `gap` property.
     * Specifies the gaps between the elements. The default value is `0`
     *
     * @public
     */
    get gap() {
        return this._gap;
    }
    set gap(value) {
        if (this._gap !== value) {
            this._gap = value;
            this.style.gap = this.generateGapStyle(this._gap);
            this.requestUpdate('gap');
        }
    }
    /**
     * Gets or sets the `horizontalAlignment` property.
     *
     * @public
     * @attr
     */
    get horizontalAlignment() {
        return this._horizontalAlignment;
    }
    set horizontalAlignment(value) {
        if (this._horizontalAlignment !== value) {
            this._horizontalAlignment = value;
            this.requestUpdate('horizontalAlignment');
        }
    }
    /**
     * Gets or sets the `verticalAlignment` property.
     *
     * @public
     * @attr
     */
    get verticalAlignment() {
        return this._verticalAlignment;
    }
    set verticalAlignment(value) {
        if (this._verticalAlignment !== value) {
            this._verticalAlignment = value;
            this.requestUpdate('verticalAlignment');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @private
     */
    generateGapStyle(gap) {
        const parsedGap = typeof gap === 'number' || typeof gap === 'string'
            ? {
                columns: gap,
                rows: gap
            }
            : {
                columns: gap.columns ?? 0,
                rows: gap.rows ?? 0
            };
        return parsedGap.rows === parsedGap.columns
            ? typeof parsedGap.rows === 'number'
                ? `${parsedGap.rows}px`
                : parsedGap.rows
            : `${typeof parsedGap.rows === 'number'
                ? `${parsedGap.rows}px`
                : parsedGap.rows} ${typeof parsedGap.columns === 'number' ? `${parsedGap.columns}px` : parsedGap.columns}`;
    }
    /**
     * @private
     */
    generateColumnStyle(definition) {
        return ColumnDefinitions.parse(definition).map((x) => {
            let style = '';
            if (x.width.isAbsolute) {
                // TODO: this here is a very dumm logic, because the value is always in px.
                return x.width
                    ? `${x.width.value}px`
                    : '0px';
            }
            if (x.width.isAuto) {
                // TODO: check this, before it was auto-fit
                return 'auto';
            }
            if (x.width.isStar) {
                return '1fr';
            }
            if (x.minWidth !== 0 && x.maxWidth !== Number.MAX_SAFE_INTEGER) {
                style += ` minmax(${x.minWidth},${x.maxWidth})`;
            }
            return style;
        })
            .join(' ');
    }
    /**
     * @private
     */
    generateRowStyle(definition) {
        return RowDefinitions.parse(definition).map((x) => {
            let style = '';
            if (x.height.isAbsolute) {
                // TODO: this here is a very dumm logic, because the value is always in px.
                style = x.height
                    ? `${x.height.value}px`
                    : '0px';
            }
            if (x.height.isAuto) {
                // TODO: check this, before it was auto-fit
                style = 'auto';
            }
            if (x.height.isStar) {
                style = 'auto-fill';
            }
            if (x.minHeight !== 0 && x.maxHeight !== Number.MAX_SAFE_INTEGER) {
                style += ` minmax(${x.minHeight},${x.maxHeight})`;
            }
            return style;
        })
            .join(' ');
    }
};
__decorate([
    Property({ type: RowDefinitions }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], GridElement.prototype, "rowDefinitions", null);
__decorate([
    Property({ type: ColumnDefinitions }),
    __metadata("design:type", Array),
    __metadata("design:paramtypes", [Array])
], GridElement.prototype, "columnDefinitions", null);
__decorate([
    Property({ type: Object }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], GridElement.prototype, "gap", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], GridElement.prototype, "horizontalAlignment", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], GridElement.prototype, "verticalAlignment", null);
GridElement = __decorate([
    Component({
        selector: 'mosaik-grid',
        themes: {
            joy: gridElementJoyStyle,
            retro: gridElementRetroStyle,
            cosmopolitan: gridElementCosmopolitanStyle
        },
        template: gridElementTemplate
    }),
    __metadata("design:paramtypes", [])
], GridElement);
export { GridElement };
//# sourceMappingURL=GridElement.js.map