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() {
        this._fixCells();
        const style = this.componentElement.nativeElement.style;
        let gridTemplateRows = '';
        let gridTemplateColumns = '';
        if (this.options?.rows && Array.isArray(this.options?.rows)) {
            for (const row of this.options.rows) {
                gridTemplateRows += `${row.height} `;
            }
        }
        if (this.options?.columns && Array.isArray(this.options?.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;
        if (!this.options) {
            return;
        }
        (_a = this.options).cells ?? (_a.cells = []);
        const grid = [];
        for (let y = 0; y < (this.options.rows ?? []).length; y++)
            for (let x = 0; x < (this.options.columns ?? []).length; x++) {
                grid[y] ?? (grid[y] = []);
                grid[y][x] = '';
            }
        for (const cell of this.options.cells) {
            cell.package = 'basic-components';
            cell.name = 'gridPanelCell';
            for (let y = (cell.options?.gridRowStart ?? 1); y < (cell.options?.gridRowEnd ?? 2); y++)
                for (let x = (cell.options?.gridColumnStart ?? 1); x < (cell.options?.gridColumnEnd ?? 2); x++) {
                    const yCoordinate = y - 1;
                    const xCoordinate = x - 1;
                    if (grid[yCoordinate][xCoordinate] === '') {
                        grid[yCoordinate][xCoordinate] = cell.id;
                    }
                }
        }
        for (let y = 1; y <= (this.options.rows ?? []).length; y++)
            for (let x = 1; x <= (this.options.columns ?? []).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 => options?.cells ?? []),
    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