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 AXMLearningManagementSkillMatrixComponent } from './acorex-modules-learning-management-skill-matrix.component-BVVtpWD_.mjs';

//#region ----   Imports   ----
//#endregion
//#region ----   Component Definition   ----
class AXLSkillMatrixWidgetEditComponent extends AXPValueWidgetComponent {
    constructor() {
        super(...arguments);
        //#endregion
        //#region ----   Computed Properties   ----
        /**
         * Titles from context - reactively reads from context data signal
         * This ensures the titles update immediately when requiredLevels changes in context
         */
        this.titles = computed(() => {
            // Read directly from context data signal for reactive updates
            const contextData = this.contextService.data();
            const requiredLevelsFromContext = get(contextData, 'jobLevels');
            if (Array.isArray(requiredLevelsFromContext) && requiredLevelsFromContext.length > 0) {
                // Ensure each item has the required structure
                return requiredLevelsFromContext.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)) {
                // Ensure each item has the required structure
                return rawTitles.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,
                    };
                });
            }
            // If it's still a string (unevaluated expression), return empty array
            // The expression will be evaluated by the widget renderer and options will update
            if (typeof rawTitles === 'string') {
                return [];
            }
            return [];
        }, ...(ngDevMode ? [{ debugName: "titles" }] : []));
        /**
         * Readonly mode from options - properly tracks options signal changes
         */
        this.readonly = computed(() => {
            const options = this.options();
            const result = options?.['readonly'] ?? false;
            return result;
        }, ...(ngDevMode ? [{ debugName: "readonly" }] : []));
        /**
         * Current matrix value - tracks getValue signal changes
         */
        this.matrixValue = computed(() => {
            const result = this.getValue() || {};
            const keysCount = Object.keys(result).length;
            return result;
        }, ...(ngDevMode ? [{ debugName: "matrixValue" }] : []));
        /**
         * Skill IDs extracted from matrix data keys or from options
         */
        this.skillIds = computed(() => {
            const options = this.options();
            const skillIdsFromOptions = options?.['skillIds'] ?? [];
            // If skillIds from options is an array, use it
            if (Array.isArray(skillIdsFromOptions) && skillIdsFromOptions.length > 0) {
                return skillIdsFromOptions;
            }
            // Otherwise, extract from matrix data keys
            const matrixData = this.matrixValue();
            return Object.keys(matrixData || {});
        }, ...(ngDevMode ? [{ debugName: "skillIds" }] : []));
    }
    //#endregion
    //#region ----   Event Handlers   ----
    /**
     * Handle value change from skill-matrix component
     */
    handleValueChange(data) {
        const dataKeysCount = Object.keys(data || {}).length;
        const currentKeysCount = Object.keys(this.getValue() || {}).length;
        if (data) {
            this.setValue(data);
            const afterKeysCount = Object.keys(this.getValue() || {}).length;
        }
        else {
        }
    }
    /**
     * Handle skill IDs change from skill-matrix component
     * Skill IDs are stored as keys in the matrix data, so this is mainly for tracking
     */
    handleSkillIdsChange(skillIds) {
        // Skill IDs are already reflected in the matrix data keys
        // This handler is here for potential future use or side effects
        console.log('Skill IDs changed:', skillIds);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXLSkillMatrixWidgetEditComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.19", type: AXLSkillMatrixWidgetEditComponent, isStandalone: true, selector: "axm-skill-matrix-widget-edit", host: { classAttribute: "ax-block ax-w-full" }, usesInheritance: true, ngImport: i0, template: `
        <axm-skill-matrix
            [titles]="titles()"
            [value]="matrixValue()"
            [skillIds]="skillIds()"
            [readonly]="readonly()"
            (valueChange)="handleValueChange($any($event))"
            (skillIdsChange)="handleSkillIdsChange($any($event))"
        ></axm-skill-matrix>
    `, isInline: true, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "component", type: AXMLearningManagementSkillMatrixComponent, selector: "axm-skill-matrix", inputs: ["titles", "value", "readonly", "skillIds"], outputs: ["valueChange", "cellChange", "skillIdsChange"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXLSkillMatrixWidgetEditComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'axm-skill-matrix-widget-edit',
                    standalone: true,
                    imports: [
                        CommonModule,
                        AXMLearningManagementSkillMatrixComponent,
                    ],
                    template: `
        <axm-skill-matrix
            [titles]="titles()"
            [value]="matrixValue()"
            [skillIds]="skillIds()"
            [readonly]="readonly()"
            (valueChange)="handleValueChange($any($event))"
            (skillIdsChange)="handleSkillIdsChange($any($event))"
        ></axm-skill-matrix>
    `,
                    host: {
                        class: 'ax-block ax-w-full',
                    },
                    encapsulation: ViewEncapsulation.None,
                    changeDetection: ChangeDetectionStrategy.OnPush,
                }]
        }] });

export { AXLSkillMatrixWidgetEditComponent };
//# sourceMappingURL=acorex-modules-learning-management-skill-matrix-widget-edit.component-DnDbFIMf.mjs.map
