import { AXPWidgetsList } from '@acorex/modules/common';
import { createAllQueryView, AXPEntityQueryType } from '@acorex/platform/common';
import { AXMEntityCrudServiceImpl, entityMasterCrudActions, entityMasterDeleteAction } from '@acorex/platform/layout/entity';
import { AXPWidgetsCatalog } from '@acorex/platform/layout/widget-core';
import { runInInjectionContext } from '@angular/core';
import { RootConfig } from './acorex-modules-asset-management.mjs';
import { RootConfig as RootConfig$1 } from '@acorex/modules/maintenance-management';

//#region ----   Imports   ----
//#endregion
//#region ----   Entity Factory   ----
/**
 * Factory function for the manualAssetCategory entity definition.
 * Modeled after the auto-generated category entity provider (entity.provider.ts)
 * to ensure full compatibility with tree view, category sidebar, and hierarchy features.
 *
 * Includes: parent hierarchy, description, appearance (icon & color), and metadata selector.
 */
async function factory(injector) {
    /** Full entity key used for CRUD data service and EntityCategory widget reference */
    const fullKey = `${RootConfig.module.name}.${RootConfig.entities.manualAssetCategory.name}`;
    /** Data service for querying and mutating entity records */
    const dataService = runInInjectionContext(injector, () => new AXMEntityCrudServiceImpl(fullKey));
    /** i18n base path for this entity */
    const i18n = '@asset-management:asset-categories.entities.asset-category';
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.manualAssetCategory.name,
        title: RootConfig.entities.manualAssetCategory.title,
        icon: RootConfig.entities.manualAssetCategory.icon,
        /** Parent key enables tree view rendering in data table and category sidebar */
        parentKey: 'parentId',
        plugins: [
            {
                name: 'meta-data-selector',
                options: {
                    title: '@asset-management:assets.entities.asset.components.meta-data-form.title',
                },
            },
        ],
        formats: {
            individual: RootConfig.entities.manualAssetCategory.title,
            plural: RootConfig.entities.manualAssetCategory.titlePlural,
            searchResult: { title: '{{ title }}', description: '{{ description }}' },
        },
        relatedEntities: [],
        //#region ----   Groups   ----
        groups: [
            { id: 'basic-info', title: `${i18n}.groups.basic-info` },
            { id: 'appearance', title: '@general:terms.interface.appearance' },
        ],
        //#endregion
        //#region ----   Properties   ----
        properties: [
            {
                name: 'title',
                title: `${i18n}.fields.title.title`,
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    unique: { enabled: true },
                    interface: { type: AXPWidgetsCatalog.text },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'parentId',
                title: '@general:terms.classification.parent',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.EntityCategory,
                        options: { entity: fullKey },
                    },
                },
            },
            {
                name: 'description',
                title: `${i18n}.fields.description.title`,
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: { type: AXPWidgetsCatalog.largeText },
                },
            },
            {
                name: 'hasMaintenance',
                title: '@asset-management:asset-categories.entities.asset-category.fields.has-maintenance.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'boolean',
                    defaultValue: false,
                    interface: {
                        type: AXPWidgetsCatalog.toggle,
                        triggers: [
                            {
                                event: "{{ events.context('hasMaintenance') }}",
                                action: `{{ if(!context.eval('hasMaintenance')) { context.set('maintenanceTypeIds', null); } }}`,
                            },
                        ],
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true } },
                },
            },
            {
                name: 'maintenanceTypeIds',
                title: '@asset-management:asset-categories.entities.asset-category.fields.maintenance-type-id.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'object',
                    visible: `{{ context.eval('hasMaintenance') }}`,
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.maintenanceType.name}`,
                            textField: 'title',
                            valueField: 'id',
                            multiple: true,
                            disabled: `{{ !context.eval('hasMaintenance') }}`,
                            expose: [
                                { source: 'id', target: 'maintenanceTypes.{id}' },
                                { source: 'title', target: 'maintenanceTypes.{title}' },
                            ],
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true } },
                },
            },
            {
                name: 'hasInspection',
                title: '@asset-management:asset-categories.entities.asset-category.fields.has-inspection.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'boolean',
                    interface: { type: AXPWidgetsCatalog.toggle },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true } },
                },
            },
            // Appearance properties
            {
                name: 'color',
                title: '@general:terms.common.color',
                groupId: 'appearance',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.ColorPalette,
                        options: {},
                    },
                },
            },
            {
                name: 'icon',
                title: '@general:terms.common.icon',
                groupId: 'appearance',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Theme.IconChooser,
                        options: {},
                    },
                },
            },
        ],
        //#endregion
        //#region ----   Columns   ----
        columns: [
            { name: 'title' },
            { name: 'description' },
            { name: 'hasMaintenance' },
            { name: 'hasInspection' },
        ],
        //#endregion
        //#region ----   Queries   ----
        queries: {
            list: {
                type: AXPEntityQueryType.List,
                execute: async (q) => {
                    console.log({ q });
                    const res = await dataService.query(q);
                    // Build a set of IDs that are referenced as parentId to dynamically compute childrenCount
                    return {
                        items: res.items.map((item) => ({
                            ...item,
                            hasChild: item.childrenCount > 0,
                        })),
                        total: res.total,
                    };
                },
            },
        },
        //#endregion
        //#region ----   Interfaces   ----
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'basic-info' }, { id: 'appearance' }],
                    properties: [
                        { name: 'title', layout: { positions: { lg: { colSpan: 12, order: 1 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 12, order: 2 } } } },
                        { name: 'description', layout: { positions: { lg: { colSpan: 12, order: 3 } } } },
                        { name: 'hasMaintenance', layout: { positions: { lg: { colSpan: 12, order: 4 } } } },
                        { name: 'maintenanceTypeIds', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'hasInspection', layout: { positions: { lg: { colSpan: 12, order: 6 } } } },
                        { name: 'color', layout: { positions: { lg: { colSpan: 12, order: 7 } } } },
                        { name: 'icon', layout: { positions: { lg: { colSpan: 12, order: 8 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'basic-info' }, { id: 'appearance' }],
                    properties: [
                        { name: 'title', layout: { positions: { lg: { colSpan: 12, order: 1 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 12, order: 2 } } } },
                        { name: 'description', layout: { positions: { lg: { colSpan: 12, order: 3 } } } },
                        { name: 'hasMaintenance', layout: { positions: { lg: { colSpan: 12, order: 4 } } } },
                        { name: 'maintenanceTypeIds', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'hasInspection', layout: { positions: { lg: { colSpan: 12, order: 6 } } } },
                        { name: 'color', layout: { positions: { lg: { colSpan: 12, order: 7 } } } },
                        { name: 'icon', layout: { positions: { lg: { colSpan: 12, order: 8 } } } },
                    ],
                },
                single: {
                    title: '{{ title }}',
                    sections: [
                        { id: 'basic-info', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'appearance', layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'title', layout: { positions: { lg: { colSpan: 12, order: 1 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 12, order: 2 } } } },
                        { name: 'description', layout: { positions: { lg: { colSpan: 12, order: 3 } } } },
                        { name: 'hasMaintenance', layout: { positions: { lg: { colSpan: 12, order: 4 } } } },
                        { name: 'maintenanceTypeIds', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'hasInspection', layout: { positions: { lg: { colSpan: 12, order: 6 } } } },
                        { name: 'color', layout: { positions: { lg: { colSpan: 12, order: 7 } } } },
                        { name: 'icon', layout: { positions: { lg: { colSpan: 12, order: 8 } } } },
                    ],
                    actions: [entityMasterDeleteAction()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [createAllQueryView({ sorts: [{ name: 'title', dir: 'asc' }] })],
                },
            },
        },
        //#endregion
    };
    return entityDef;
}
//#endregion

export { factory };
//# sourceMappingURL=acorex-modules-asset-management-manual-asset-category.entity-DyIV_vbE.mjs.map
