import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { computed, ChangeDetectionStrategy, ViewEncapsulation, Component } from '@angular/core';
import { AXPValueWidgetComponent } from '@acorex/platform/layout/widget-core';
import { get } from 'lodash-es';
import { A as AXMOrganizationManagementResponsibilitiesMatrixComponent } from './acorex-modules-organization-management-responsibilities-matrix.component-BM-jMzZf.mjs';

//#region ----   Imports   ----
//#endregion
//#region ----   Component Definition   ----
class AXMResponsibilitiesMatrixWidgetEditComponent extends AXPValueWidgetComponent {
    constructor() {
        super(...arguments);
        //#endregion
        //#region ----   Computed Properties   ----
        /**
         * Readonly mode from options
         */
        this.readonly = computed(() => {
            const options = this.options();
            return options?.['readonly'] ?? false;
        }, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
        /**
         * Titles from context - reactively reads from context data signal
         * This ensures the titles update immediately when jobLevels changes in context
         */
        this.titles = computed(() => {
            // Read directly from context data signal for reactive updates
            const contextData = this.contextService.data();
            const jobLevelsFromContext = get(contextData, 'jobLevels');
            if (Array.isArray(jobLevelsFromContext) && jobLevelsFromContext.length > 0) {
                // Ensure each item has the required structure
                return jobLevelsFromContext.map((item) => {
                    if (typeof item === 'string') {
                        // If item is just a string (ID), convert to proper format
                        return { id: item, title: item };
                    }
                    // If item is an object, ensure it has id and title
                    return {
                        id: item.id || item.value || String(item),
                        title: item.title || item.name || String(item),
                        description: item.description,
                    };
                });
            }
            // Fallback to options (for initial load or when context is not yet available)
            const options = this.options();
            const rawTitles = options?.['titles'];
            // If titles is not set, return empty array
            if (!rawTitles) {
                return [];
            }
            // If it's already an array, return it (evaluated expression result)
            if (Array.isArray(rawTitles)) {
                return rawTitles.map((item) => {
                    if (typeof item === 'string') {
                        return { id: item, title: item };
                    }
                    return {
                        id: item.id || item.value || String(item),
                        title: item.title || item.name || String(item),
                        description: item.description,
                    };
                });
            }
            return [];
        }, ...(ngDevMode ? [{ debugName: "titles" }] : []));
        /**
         * Current matrix value
         */
        this.matrixValue = computed(() => {
            return this.getValue() || {};
        }, ...(ngDevMode ? [{ debugName: "matrixValue" }] : []));
        /**
         * Responsibility IDs extracted from value or from options
         */
        this.responsibilityIds = computed(() => {
            const options = this.options();
            const responsibilityIdsFromOptions = options?.['responsibilityIds'] ?? [];
            // If responsibilityIds from options is an array, use it
            if (Array.isArray(responsibilityIdsFromOptions) && responsibilityIdsFromOptions.length > 0) {
                return responsibilityIdsFromOptions;
            }
            // Otherwise, extract from value (only if value exists and has items)
            const value = this.matrixValue();
            if (value && typeof value === 'object' && Object.keys(value).length > 0) {
                return Object.keys(value).filter((id) => id != null);
            }
            // Return empty array to prevent undefined issues
            return [];
        }, ...(ngDevMode ? [{ debugName: "responsibilityIds" }] : []));
    }
    //#endregion
    //#region ----   Event Handlers   ----
    /**
     * Handle value change from responsibilities-matrix component
     */
    handleValueChange(data) {
        if (data) {
            this.setValue(data);
        }
    }
    /**
     * Handle responsibility IDs change from responsibilities-matrix component
     */
    handleResponsibilityIdsChange(responsibilityIds) {
        // Responsibility IDs are already reflected in the value
        // This handler is here for potential future use or side effects
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMResponsibilitiesMatrixWidgetEditComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.19", type: AXMResponsibilitiesMatrixWidgetEditComponent, isStandalone: true, selector: "axm-responsibilities-matrix-widget-edit", host: { classAttribute: "ax-block ax-w-full" }, usesInheritance: true, ngImport: i0, template: `
        <axm-responsibilities-matrix
            [titles]="titles()"
            [value]="matrixValue()"
            [responsibilityIds]="responsibilityIds()"
            [readonly]="readonly()"
            (valueChange)="handleValueChange($any($event))"
            (responsibilityIdsChange)="handleResponsibilityIdsChange($any($event))"
        ></axm-responsibilities-matrix>
    `, isInline: true, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "component", type: AXMOrganizationManagementResponsibilitiesMatrixComponent, selector: "axm-responsibilities-matrix", inputs: ["titles", "value", "readonly", "responsibilityIds"], outputs: ["valueChange", "cellChange", "responsibilityIdsChange"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMResponsibilitiesMatrixWidgetEditComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'axm-responsibilities-matrix-widget-edit',
                    standalone: true,
                    imports: [
                        CommonModule,
                        AXMOrganizationManagementResponsibilitiesMatrixComponent,
                    ],
                    template: `
        <axm-responsibilities-matrix
            [titles]="titles()"
            [value]="matrixValue()"
            [responsibilityIds]="responsibilityIds()"
            [readonly]="readonly()"
            (valueChange)="handleValueChange($any($event))"
            (responsibilityIdsChange)="handleResponsibilityIdsChange($any($event))"
        ></axm-responsibilities-matrix>
    `,
                    host: {
                        class: 'ax-block ax-w-full',
                    },
                    encapsulation: ViewEncapsulation.None,
                    changeDetection: ChangeDetectionStrategy.OnPush,
                }]
        }] });

export { AXMResponsibilitiesMatrixWidgetEditComponent };
//# sourceMappingURL=acorex-modules-organization-management-responsibilities-matrix-widget-edit.component-DIyIK3IN.mjs.map
