import { createAllQueryView, AXPEntityCommandScope } from '@acorex/platform/common';
import { AXPSystemActionType } from '@acorex/platform/core';
import { AXMEntityCrudServiceImpl, entityMasterCrudActions, entityMasterRecordActions, entityDetailsCrudActions, entityDetailsSimpleCondition } 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$2 } from '@acorex/modules/maintenance-management';
import { RootConfig as RootConfig$1 } from '@acorex/modules/assessment-management';
import { AXPWidgetsList } from '@acorex/modules/common';

//#region ----   Imports   ----
//#endregion
async function factory(injector) {
    //#region ----   Services & Data Enrichment   ----
    /** Asset data service */
    const assetFullKey = `${RootConfig.module.name}.${RootConfig.entities.asset.name}`;
    const assetService = runInInjectionContext(injector, () => new AXMEntityCrudServiceImpl(assetFullKey));
    /** Category data service for looking up ManualAssetCategory records */
    const categoryFullKey = `${RootConfig.module.name}.${RootConfig.entities.manualAssetCategory.name}`;
    const categoryService = runInInjectionContext(injector, () => new AXMEntityCrudServiceImpl(categoryFullKey));
    /**
     * Loads all ManualAssetCategory records (cached for reuse).
     */
    let allCategoriesCache = null;
    const loadAllCategories = async () => {
        if (allCategoriesCache)
            return allCategoriesCache;
        const result = await categoryService.query({ skip: 0, take: 1000 });
        allCategoriesCache = result.items ?? [];
        return allCategoriesCache;
    };
    //#endregion
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.asset.name,
        title: RootConfig.entities.asset.title,
        icon: RootConfig.entities.asset.icon,
        parentKey: 'parentId',
        plugins: [
            { name: 'history' },
            { name: 'compare' },
            { name: 'unique-fields', options: { fields: 'code', scope: 'global' } },
            { name: 'notes', options: { order: 1 } },
            { name: 'category', options: { categoryEntity: `AssetManagement.ManualAssetCategory`, order: 2 } },
            {
                name: 'meta-data-form',
                options: {
                    path: 'categoryIds',
                    title: '@asset-management:assets.entities.asset.components.meta-data-form.title',
                    order: 3
                },
            },
            {
                name: 'status',
                options: {
                    field: 'statusId',
                    definition: 'asset-management.status.asset',
                    order: 4
                },
            },
            { name: 'tags' },
            { name: 'attachments' },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig$2.module.name}.${RootConfig$2.entities.maintenance.name}`,
                title: '@asset-management:assets.entities.asset.related-entities.maintenances.title',
                icon: RootConfig$2.entities.maintenance.icon,
                hidden: '{{ !context.eval("hasMaintenance") }}',
                conditions: [entityDetailsSimpleCondition('resourceId')],
                actions: [
                    // Custom create action that passes resourceId AND resource data (including availableMaintenanceTypeIds)
                    {
                        title: '@general:actions.create.title',
                        command: {
                            name: 'Entity:Create',
                            options: {
                                process: {
                                    redirect: false,
                                    canCreateNewOne: true,
                                    data: {
                                        resourceId: '{{context.eval("id")}}',
                                        resource: {
                                            id: '{{context.eval("id")}}',
                                            title: '{{context.eval("title")}}',
                                            code: '{{context.eval("code")}}',
                                            availableMaintenanceTypeIds: '{{context.eval("availableMaintenanceTypeIds")}}',
                                        },
                                    },
                                },
                            },
                        },
                        priority: 'primary',
                        type: AXPSystemActionType.Create,
                        scope: AXPEntityCommandScope.TypeLevel,
                    },
                    // Keep edit, view actions from standard helpers
                    ...entityDetailsCrudActions('resourceId', { create: false }),
                ],
                layout: { type: 'page-list', position: 'after', order: 1 },
            },
            // {
            //   entity: `${RootConfig.module.name}.${RootConfig.entities.inspection.name}`,
            //   title: '@asset-management:assets.entities.asset.related-entities.inspections.title',
            //   icon: RootConfig.entities.inspection.icon,
            //   conditions: [entityDetailsSimpleCondition('assetId')],
            //   actions: [...entityDetailsCrudActions('assetId')],
            //   layout: { type: 'page-list', position: 'after', order: 2 },
            // },
        ],
        formats: {
            individual: RootConfig.entities.asset.title,
            plural: RootConfig.entities.asset.titlePlural,
            searchResult: { title: '{{ title }}' },
            displayName: '@asset-management:assets.entities.asset.title',
        },
        groups: [
            { id: 'basic-info', title: '@asset-management:assets.entities.asset.groups.basic-info' },
            { id: 'category-info', title: '@asset-management:assets.entities.asset.groups.category-info' },
            { id: 'ownership', title: '@asset-management:assets.entities.asset.groups.ownership' },
            { id: 'placement', title: '@asset-management:assets.entities.asset.groups.placement' },
            { id: 'lifecycle', title: '@asset-management:assets.entities.asset.groups.lifecycle' },
            { id: 'booking', title: '@asset-management:assets.entities.asset.groups.booking' },
            { id: 'media', title: '@asset-management:assets.entities.asset.groups.media' },
        ],
        properties: [
            {
                name: 'id',
                title: 'ID',
                schema: { dataType: 'uuid', visible: false, nullable: false, readonly: true, unique: { enabled: true } },
            },
            {
                name: 'code',
                title: '@asset-management:assets.entities.asset.fields.code.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    unique: { enabled: true },
                    interface: {
                        type: AXPWidgetsCatalog.text,
                        options: {
                            placeholder: '@asset-management:assets.entities.asset.fields.code.placeholder',
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'title',
                title: '@asset-management:assets.entities.asset.fields.title.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.text,
                        options: {
                            placeholder: '@asset-management:assets.entities.asset.fields.title.placeholder',
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'serialNumber',
                title: '@asset-management:assets.entities.asset.fields.serial-number.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.text,
                        options: {
                            placeholder: '@asset-management:assets.entities.asset.fields.serial-number.placeholder',
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'testTagIdentificationNumber',
                title: '@asset-management:assets.entities.asset.fields.test-tag-identification-number.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.text,
                        options: {
                            placeholder: '@asset-management:assets.entities.asset.fields.test-tag-identification-number.placeholder',
                        },
                    },
                },
            },
            {
                name: 'qrCode',
                title: '@asset-management:assets.entities.asset.fields.qr-code.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.qrcode,
                        options: {
                            size: 200,
                            outputType: 'svg',
                            readonly: true,
                        },
                        triggers: [
                            {
                                event: '{{ events.context("code") }}',
                                action: '{{ context.set("qrCode", context.eval("code")) }}',
                            },
                        ],
                    },
                },
            },
            // {
            //   name: 'description',
            //   title: '@asset-management:assets.entities.asset.fields.description.title',
            //   groupId: 'basic-info',
            //   schema: {
            //     dataType: 'string',
            //     interface: {
            //       type: AXPWidgetsCatalog.largeText,
            //       options: {
            //         rows: 4,
            //         placeholder: '@asset-management:assets.entities.asset.fields.description.placeholder',
            //       },
            //     },
            //   },
            //   options: {
            //     sort: { enabled: false },
            //     filter: { advance: { enabled: true }, inline: { enabled: false } },
            //   },
            // },
            {
                name: 'isRented',
                title: '@asset-management:assets.entities.asset.fields.is-rented.title',
                groupId: 'basic-info',
                schema: {
                    dataType: 'boolean',
                    interface: {
                        type: AXPWidgetsCatalog.toggle,
                    },
                },
            },
            {
                name: 'ownerId',
                title: '@asset-management:assets.entities.asset.fields.owner-id.title',
                groupId: 'ownership',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: 'HumanCapitalManagement.Employee',
                            textField: 'person.displayName',
                            valueField: 'id',
                            columns: ['person.displayName', 'employeeCode'],
                            expose: [
                                { source: 'id', target: 'owner.id' },
                                { source: 'person.displayName', target: 'owner.displayName' },
                            ],
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'assignedToEmployeeId',
                title: '@asset-management:assets.entities.asset.fields.assigned-to-employee-id.title',
                groupId: 'ownership',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: 'HumanCapitalManagement.Employee',
                            textField: 'person.displayName',
                            valueField: 'id',
                            columns: ['person.displayName', 'employeeCode'],
                            expose: [
                                { source: 'id', target: 'assignedToEmployee.id' },
                                { source: 'person.displayName', target: 'assignedToEmployee.displayName' },
                            ],
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'locationId',
                title: '@asset-management:assets.entities.asset.fields.location-id.title',
                groupId: 'placement',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: 'LocationManagement.Location',
                            textField: 'title',
                            valueField: 'id',
                            expose: [
                                { source: 'id', target: 'location.id' },
                                { source: 'title', target: 'location.title' },
                            ],
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'parentId',
                title: '@asset-management:assets.entities.asset.fields.parent-id.title',
                groupId: 'placement',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig.module.name}.Asset`,
                            textField: 'title',
                            valueField: 'id',
                            expose: [
                                { source: 'id', target: 'parent.id' },
                                { source: 'title', target: 'parent.title' },
                            ],
                        },
                    },
                },
            },
            {
                name: 'purchaseDate',
                title: '@asset-management:assets.entities.asset.fields.purchase-date.title',
                groupId: 'lifecycle',
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsCatalog.dateTime,
                        options: {
                            format: 'date',
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'commissionedDate',
                title: '@asset-management:assets.entities.asset.fields.commissioned-date.title',
                groupId: 'lifecycle',
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsCatalog.dateTime,
                        options: {
                            format: 'date',
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'retiredDate',
                title: '@asset-management:assets.entities.asset.fields.retired-date.title',
                groupId: 'lifecycle',
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsCatalog.dateTime,
                        options: {
                            format: 'date',
                        },
                    },
                },
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
            },
            {
                name: 'isBookable',
                title: '@asset-management:assets.entities.asset.fields.is-bookable.title',
                groupId: 'booking',
                schema: {
                    dataType: 'boolean',
                    interface: {
                        type: AXPWidgetsCatalog.toggle,
                    },
                },
            },
            {
                name: 'preBookQuestionnaireId',
                title: '@asset-management:assets.entities.asset.fields.pre-book-questionnaire-id.title',
                groupId: 'booking',
                schema: {
                    dataType: 'string',
                    visible: `{{ context.eval('isBookable') }}`,
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.questionnaire.name}`,
                            textField: 'title',
                            valueField: 'id',
                            expose: [
                                { source: 'id', target: 'preBookQuestionnaire.id' },
                                { source: 'title', target: 'preBookQuestionnaire.title' },
                            ],
                        },
                    },
                },
            },
            {
                name: 'postBookQuestionnaireId',
                title: '@asset-management:assets.entities.asset.fields.post-book-questionnaire-id.title',
                groupId: 'booking',
                schema: {
                    dataType: 'string',
                    visible: `{{ context.eval('isBookable') }}`,
                    interface: {
                        type: AXPWidgetsCatalog.lookup,
                        options: {
                            entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.questionnaire.name}`,
                            textField: 'title',
                            valueField: 'id',
                            expose: [
                                { source: 'id', target: 'postBookQuestionnaire.id' },
                                { source: 'title', target: 'postBookQuestionnaire.title' },
                            ],
                        },
                    },
                },
            },
            {
                name: 'images',
                title: '@asset-management:assets.entities.asset.fields.images.title',
                groupId: 'media',
                options: {
                    sort: { enabled: false },
                    filter: {
                        advance: { enabled: false },
                        inline: { enabled: false },
                    },
                },
                schema: {
                    dataType: 'array',
                    interface: {
                        type: AXPWidgetsCatalog.gallery,
                    },
                },
            },
            {
                name: 'availableMaintenanceTypeIds',
                title: 'Available Maintenance Types',
                groupId: 'category-info',
                schema: { dataType: 'array', visible: false, readonly: true },
            },
            {
                name: 'hasMaintenance',
                title: 'Has Maintenance',
                groupId: 'category-info',
                schema: { dataType: 'boolean', visible: false, readonly: true },
            },
        ],
        columns: [
            { name: 'code' },
            { name: 'title' },
            { name: 'serialNumber' },
            // { name: 'description' },
            { name: 'ownerId', options: { dataPath: 'owner.displayName' } },
            { name: 'assignedToEmployeeId', options: { dataPath: 'assignedToEmployee.displayName' } },
            { name: 'locationId', options: { dataPath: 'location.title' } },
            { name: 'statusId' },
            { name: 'purchaseDate' },
            { name: 'commissionedDate' },
            { name: 'retiredDate' },
            { name: 'isBookable' },
            { name: 'qrCode' },
        ],
        queries: {},
        interfaces: {
            master: {
                create: {
                    sections: [
                        { id: 'basic-info', order: 1 },
                        { id: 'category-info', order: 2 },
                        { id: 'ownership', order: 3 },
                        { id: 'placement', order: 4 },
                        { id: 'lifecycle', order: 5 },
                        { id: 'booking', order: 6 },
                        { id: 'media', order: 7 },
                    ],
                    properties: [
                        { name: 'code', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'title', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'serialNumber', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'testTagIdentificationNumber', layout: { positions: { lg: { colSpan: 6, order: 4 } } } },
                        { name: 'isRented', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'ownerId', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'assignedToEmployeeId', layout: { positions: { lg: { colSpan: 6, order: 7 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'purchaseDate', layout: { positions: { lg: { colSpan: 6, order: 10 } } } },
                        { name: 'commissionedDate', layout: { positions: { lg: { colSpan: 6, order: 11 } } } },
                        { name: 'retiredDate', layout: { positions: { lg: { colSpan: 6, order: 12 } } } },
                        { name: 'isBookable', layout: { positions: { lg: { colSpan: 12, order: 13 } } } },
                        { name: 'preBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 14 } } } },
                        { name: 'postBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 15 } } } },
                        {
                            name: 'qrCode',
                            layout: { positions: { lg: { colSpan: 6, rowSpan: 4, order: 16 } } },
                        },
                        { name: 'images', layout: { positions: { lg: { colSpan: 12, order: 17 } } } },
                    ],
                },
                update: {
                    sections: [
                        { id: 'basic-info', order: 1 },
                        { id: 'category-info', order: 2 },
                        { id: 'ownership', order: 3 },
                        { id: 'placement', order: 4 },
                        { id: 'booking', order: 5 },
                        { id: 'media', order: 6 },
                    ],
                    properties: [
                        { name: 'code', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'title', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'serialNumber', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'testTagIdentificationNumber', layout: { positions: { lg: { colSpan: 6, order: 4 } } } },
                        { name: 'isRented', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'ownerId', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'assignedToEmployeeId', layout: { positions: { lg: { colSpan: 6, order: 7 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'purchaseDate', layout: { positions: { lg: { colSpan: 6, order: 10 } } } },
                        { name: 'commissionedDate', layout: { positions: { lg: { colSpan: 6, order: 11 } } } },
                        { name: 'retiredDate', layout: { positions: { lg: { colSpan: 6, order: 12 } } } },
                        { name: 'isBookable', layout: { positions: { lg: { colSpan: 12, order: 13 } } } },
                        { name: 'preBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 14 } } } },
                        { name: 'postBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 15 } } } },
                        {
                            name: 'qrCode',
                            layout: { positions: { lg: { colSpan: 6, rowSpan: 4, order: 16 } } },
                        },
                        { name: 'images', layout: { positions: { lg: { colSpan: 12, order: 17 } } } },
                    ],
                },
                single: {
                    title: '{{ title }}',
                    sections: [
                        { id: 'basic-info', order: 1 },
                        { id: 'ownership', order: 2 },
                        { id: 'placement', order: 3 },
                        { id: 'lifecycle', order: 4 },
                        { id: 'booking', order: 5 },
                        { id: 'media', order: 6 },
                    ],
                    properties: [
                        { name: 'code', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'title', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'serialNumber', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'testTagIdentificationNumber', layout: { positions: { lg: { colSpan: 6, order: 4 } } } },
                        { name: 'isRented', layout: { positions: { lg: { colSpan: 12, order: 5 } } } },
                        { name: 'ownerId', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'assignedToEmployeeId', layout: { positions: { lg: { colSpan: 6, order: 7 } } } },
                        { name: 'parentId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'purchaseDate', layout: { positions: { lg: { colSpan: 4, order: 10 } } } },
                        { name: 'commissionedDate', layout: { positions: { lg: { colSpan: 4, order: 11 } } } },
                        { name: 'retiredDate', layout: { positions: { lg: { colSpan: 4, order: 12 } } } },
                        { name: 'isBookable', layout: { positions: { lg: { colSpan: 12, order: 13 } } } },
                        { name: 'preBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 14 } } } },
                        { name: 'postBookQuestionnaireId', layout: { positions: { lg: { colSpan: 6, order: 15 } } } },
                        {
                            name: 'qrCode',
                            layout: { positions: { lg: { colSpan: 6, rowSpan: 4, order: 16 } } },
                        },
                        { name: 'images', layout: { positions: { lg: { colSpan: 12, order: 17 } } } },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [createAllQueryView({ sorts: [{ name: 'title', dir: 'asc' }] })],
                },
            },
        },
    };
    return entityDef;
}

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