import { __decorate } from "tslib";
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LayoutComponentBase, LayoutComponentRendererSADirective } from '@anglr/dynamic/layout';
import { DescendantsGetter, LayoutEditorMetadata } from '@anglr/dynamic/layout-editor';
import { generateId } from '@jscrpt/common';
import { GridPanelLayoutMetadataLoader } from './gridPanel.metadata';
import * as i0 from "@angular/core";
import * as i1 from "@angular/common";
/**
 * Component used for displaying grid panel layout
 */
let GridPanelSAComponent = class GridPanelSAComponent extends LayoutComponentBase {
    //######################### protected methods - overrides #########################
    /**
     * @inheritdoc
     */
    onOptionsSet() {
        var _a, _b, _c, _d;
        this._fixCells();
        const style = this.componentElement.nativeElement.style;
        let gridTemplateRows = '';
        let gridTemplateColumns = '';
        if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.rows) && Array.isArray((_b = this.options) === null || _b === void 0 ? void 0 : _b.rows)) {
            for (const row of this.options.rows) {
                gridTemplateRows += `${row.height} `;
            }
        }
        if (((_c = this.options) === null || _c === void 0 ? void 0 : _c.columns) && Array.isArray((_d = this.options) === null || _d === void 0 ? void 0 : _d.columns)) {
            for (const column of this.options.columns) {
                gridTemplateColumns += `${column.width} `;
            }
        }
        style.gridTemplateRows = gridTemplateRows.trim();
        style.gridTemplateColumns = gridTemplateColumns.trim();
    }
    //######################### protected methods #########################
    /**
     * Fixes cell metadata
     */
    _fixCells() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
        var _q;
        if (!this.options) {
            return;
        }
        (_a = (_q = this.options).cells) !== null && _a !== void 0 ? _a : (_q.cells = []);
        const grid = [];
        for (let y = 0; y < ((_b = this.options.rows) !== null && _b !== void 0 ? _b : []).length; y++)
            for (let x = 0; x < ((_c = this.options.columns) !== null && _c !== void 0 ? _c : []).length; x++) {
                (_d = grid[y]) !== null && _d !== void 0 ? _d : (grid[y] = []);
                grid[y][x] = '';
            }
        for (const cell of this.options.cells) {
            cell.package = 'basic-components';
            cell.name = 'gridPanelCell';
            for (let y = ((_f = (_e = cell.options) === null || _e === void 0 ? void 0 : _e.gridRowStart) !== null && _f !== void 0 ? _f : 1); y < ((_h = (_g = cell.options) === null || _g === void 0 ? void 0 : _g.gridRowEnd) !== null && _h !== void 0 ? _h : 2); y++)
                for (let x = ((_k = (_j = cell.options) === null || _j === void 0 ? void 0 : _j.gridColumnStart) !== null && _k !== void 0 ? _k : 1); x < ((_m = (_l = cell.options) === null || _l === void 0 ? void 0 : _l.gridColumnEnd) !== null && _m !== void 0 ? _m : 2); x++) {
                    const yCoordinate = y - 1;
                    const xCoordinate = x - 1;
                    if (grid[yCoordinate][xCoordinate] === '') {
                        grid[yCoordinate][xCoordinate] = cell.id;
                    }
                }
        }
        for (let y = 1; y <= ((_o = this.options.rows) !== null && _o !== void 0 ? _o : []).length; y++)
            for (let x = 1; x <= ((_p = this.options.columns) !== null && _p !== void 0 ? _p : []).length; x++) {
                if (grid[y - 1][x - 1] === '') {
                    this.options.cells.push({
                        id: `${generateId(6)}-r${y}-${y + 1}-c${x}-${x + 1}`,
                        package: 'basic-components',
                        name: 'gridPanelCell',
                        options: {
                            gridRowStart: y,
                            gridRowEnd: y + 1,
                            gridColumnStart: x,
                            gridColumnEnd: x + 1
                        }
                    });
                }
            }
    }
};
GridPanelSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: GridPanelSAComponent, deps: null, target: i0.ɵɵFactoryTarget.Component });
GridPanelSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: GridPanelSAComponent, isStandalone: true, selector: "grid-panel", usesInheritance: true, ngImport: i0, template: "<ng-container *ngFor=\"let type of options?.cells\">\n    <ng-template [layoutComponentRenderer]=\"type\"></ng-template>\n</ng-container>", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: LayoutComponentRendererSADirective, selector: "[layoutComponentRenderer]", inputs: ["layoutComponentRenderer", "layoutComponentRendererInjector", "layoutComponentRendererDisableTransformer"], outputs: ["layoutComponentRendererComponentChange", "componentElementChange"], exportAs: ["layoutComponentRenderer"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
GridPanelSAComponent = __decorate([
    DescendantsGetter(options => { var _a; return (_a = options === null || options === void 0 ? void 0 : options.cells) !== null && _a !== void 0 ? _a : []; }),
    LayoutEditorMetadata(GridPanelLayoutMetadataLoader)
], GridPanelSAComponent);
export { GridPanelSAComponent };
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: GridPanelSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'grid-panel', standalone: true, imports: [
                        CommonModule,
                        LayoutComponentRendererSADirective,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<ng-container *ngFor=\"let type of options?.cells\">\n    <ng-template [layoutComponentRenderer]=\"type\"></ng-template>\n</ng-container>" }]
        }] });
//# sourceMappingURL=gridPanel.component.js.map