import { UMB_BLOCK_GRID_AREA_TYPE_ENTRIES_CONTEXT } from '../../property-editors/block-grid-areas-config/block-grid-area-type-entries.context-token.js';
import { UmbBlockGridScaleManager, } from '../../context/block-grid-scale-manager/block-grid-scale-manager.controller.js';
import { UMB_BLOCK_GRID_AREA_CONFIG_ENTRY_CONTEXT } from './block-grid-area-config-entry.context-token.js';
import { UmbObjectState, appendToFrozenArray } from '../../../../../libs/observable-api/index.js';
import { UmbContextBase } from '../../../../../libs/class-api/index.js';
import { UMB_PROPERTY_CONTEXT } from '../../../../core/property/index.js';
import { umbConfirmModal } from '../../../../core/modal/index.js';
export class UmbBlockGridAreaConfigEntryContext extends UmbContextBase {
    //
    #entriesContext;
    //
    #propertyContext;
    //
    #areaKey;
    #area;
    getMinMaxRowSpan() {
        return [1, 999];
    }
    setColumnSpan(columnSpan) {
        const layoutColumns = this.#entriesContext?.getLayoutColumns();
        if (!layoutColumns)
            return;
        columnSpan = Math.max(1, Math.min(columnSpan, layoutColumns));
        this.#area.update({ columnSpan });
    }
    getColumnSpan() {
        return this.#area.getValue()?.columnSpan;
    }
    setRowSpan(rowSpan) {
        this.#area.update({ rowSpan });
    }
    getRowSpan() {
        return this.#area.getValue()?.rowSpan;
    }
    getAlias() {
        return this.#area.getValue()?.alias;
    }
    getRelevantColumnSpanOptions() {
        const layoutColumns = this.#entriesContext?.getLayoutColumns();
        if (!layoutColumns)
            return;
        // Makes an array based of the given length, with number entries starting from 1. ex.: length 4 => [1, 2, 3, 4]
        return Array.from({ length: layoutColumns }, (_, i) => i + 1);
    }
    constructor(host) {
        super(host, UMB_BLOCK_GRID_AREA_CONFIG_ENTRY_CONTEXT);
        this.#area = new UmbObjectState(undefined);
        this.area = this.#area.asObservable();
        this.alias = this.#area.asObservablePart((x) => x?.alias);
        this.columnSpan = this.#area.asObservablePart((x) => x?.columnSpan);
        this.rowSpan = this.#area.asObservablePart((x) => x?.rowSpan ?? 1);
        this.scaleManager = new UmbBlockGridScaleManager(this);
        this.consumeContext(UMB_PROPERTY_CONTEXT, (context) => {
            this.#propertyContext = context;
            this.#observeAreaData();
        });
        this.consumeContext(UMB_BLOCK_GRID_AREA_TYPE_ENTRIES_CONTEXT, (context) => {
            this.#entriesContext = context;
            this.scaleManager.setEntriesContext(context);
        });
    }
    setAreaKey(areaKey) {
        if (this.#areaKey === areaKey)
            return;
        this.#areaKey = areaKey;
        this.#observeAreaData();
    }
    #observeAreaData() {
        if (!this.#areaKey || !this.#propertyContext)
            return;
        this.observe(this.#propertyContext.value, (value) => {
            if (value) {
                const areaType = value.find((x) => x.key === this.#areaKey);
                this.#area.setValue(areaType);
            }
        }, 'observeAreaData');
        this.observe(this.area, (area) => {
            if (area && this.#propertyContext) {
                const value = this.#propertyContext.getValue();
                if (!value)
                    return;
                const newValue = appendToFrozenArray(value, area, (x) => x.key === this.#areaKey);
                this.#propertyContext?.setValue(newValue);
            }
        }, 'observeInternalArea');
    }
    async requestDelete() {
        await umbConfirmModal(this, {
            headline: `Delete ${this.getAlias()}`,
            content: 'Are you sure you want to delete this Area?',
            confirmLabel: 'Delete',
            color: 'danger',
        });
        this.delete();
    }
    delete() {
        if (!this.#areaKey || !this.#propertyContext)
            return;
        const value = this.#propertyContext.getValue();
        if (!value)
            return;
        this.#propertyContext.setValue(value.filter((x) => x.key !== this.#areaKey));
    }
    destroy() {
        super.destroy();
        this.#area.destroy();
    }
}
