import { AXPWidgetsList } from '@acorex/modules/common';
import { RootConfig as RootConfig$1 } from '@acorex/modules/person-management';
import { createAllQueryView, AXPEntityCommandScope } from '@acorex/platform/common';
import { entityMasterCrudActions, entityMasterRecordActions } from '@acorex/platform/layout/entity';
import { AXPWidgetsCatalog } from '@acorex/platform/layout/widget-core';
import { RootConfig } from './acorex-modules-customer-management.mjs';

async function customerFactory(injector) {
    const i18n = RootConfig.config.i18n + ':customers.entities.customer';
    const entityName = 'customer';
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.customer.name,
        title: RootConfig.entities.customer.title,
        icon: RootConfig.entities.customer.icon,
        formats: {
            individual: RootConfig.entities.customer.title,
            plural: RootConfig.entities.customer.titlePlural,
            displayName: 'Personal Information',
            searchResult: {
                title: '{{ person.displayName }}',
                description: RootConfig.module.title,
            },
        },
        plugins: [
            { name: 'identifier', options: { field: 'customerCode', rule: 'customer-code' } },
            { name: 'history' },
            { name: 'lock' },
            { name: 'compare' },
            { name: 'attachments', options: { accept: '.jpg,.jpeg,.png,.pdf', multiple: true, fileEditable: true } },
            { name: 'archive' },
            { name: 'notes' },
            // { name: 'comments' },
        ],
        groups: [
            { id: 'basic-info', title: `@${i18n}.groups.basic-info` },
            { id: 'customer-profile', title: `@${i18n}.groups.customer-profile` },
            { id: 'additional-info', title: '@general:terms.interface.additional-info' },
        ],
        properties: [
            {
                name: 'id',
                title: 'ID',
                groupId: 'basic-info',
                schema: { dataType: 'uuid', visible: false, nullable: false, readonly: true, unique: { enabled: true } },
            },
            {
                name: 'customerCode',
                title: `@${i18n}.fields.customer-code.title`,
                groupId: 'customer-profile',
                schema: { dataType: 'string', interface: { type: AXPWidgetsCatalog.text } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'customerTypeId',
                title: `@${i18n}.fields.customer-type.title`,
                groupId: 'customer-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'BusinessCore.PartyType',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'partyType.id' },
                                { source: 'title', target: 'partyType.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'segmentId',
                title: `@${i18n}.fields.segment.title`,
                groupId: 'customer-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'BusinessCore.PartySegment',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'partySegment.id' },
                                { source: 'title', target: 'partySegment.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'lifecycleStageId',
                title: `@${i18n}.fields.lifecycle-stage.title`,
                groupId: 'customer-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'BusinessCore.LifecycleStage',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'lifecycleStage.id' },
                                { source: 'title', target: 'lifecycleStage.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'territoryId',
                title: `@${i18n}.fields.territory.title`,
                groupId: 'customer-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'LocationManagement.GeographicRegion',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'territory.id' },
                                { source: 'title', target: 'territory.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'status',
                title: '@general:terms.status.title',
                groupId: 'customer-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.SelectBox,
                        options: {
                            valueField: 'id',
                            dataSource: [
                                { id: 'active', title: 'Active' },
                                { id: 'inactive', title: 'Inactive' },
                                { id: 'blocked', title: 'Blocked' },
                                { id: 'archived', title: 'Archived' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'since',
                title: `@${i18n}.fields.since.title`,
                groupId: 'customer-profile',
                schema: { dataType: 'date', interface: { type: AXPWidgetsCatalog.dateTime } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'isVip',
                title: `@${i18n}.fields.is-vip.title`,
                groupId: 'customer-profile',
                schema: { dataType: 'boolean', interface: { type: AXPWidgetsCatalog.toggle } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'accountManagerId',
                title: `@${i18n}.fields.account-manager.title`,
                groupId: 'additional-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `HumanCapitalManagement.Employee`,
                            textField: 'person.displayName',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'accountManager.id' },
                                { source: 'employeeCode', target: 'accountManager.employeeCode' },
                                { source: 'person.displayName', target: 'accountManager.displayName' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: false }, filter: { advance: { enabled: true }, inline: { enabled: false } } },
            },
        ],
        columns: [
            {
                name: 'person.displayName',
                title: '@general:terms.common.name',
                options: { width: '300px' },
                showAs: { type: AXPWidgetsCatalog.text },
            },
            { name: 'customerCode' },
            { name: 'customerTypeId', options: { dataPath: 'partyType.title' } },
            { name: 'segmentId', options: { dataPath: 'partySegment.title' } },
            { name: 'lifecycleStageId', options: { dataPath: 'lifecycleStage.title' } },
            { name: 'territoryId', options: { dataPath: 'territory.title' } },
            { name: 'status' },
            { name: 'since' },
            { name: 'isVip' },
            { name: 'accountManagerId', options: { dataPath: 'accountManager.person.displayName' } },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.person.name}`,
                title: 'Basic Information',
                conditions: [
                    {
                        name: 'id',
                        operator: { type: 'equal' },
                        value: '{{ context.eval("personId") }}',
                    },
                ],
                layout: { type: 'merge-detail' },
                columns: ['displayName', 'email', 'phone', 'address'],
                persistence: {
                    dataPath: 'person',
                    strategy: 'referenced',
                    idField: 'personId',
                },
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.opportunity.name}`,
                title: RootConfig.entities.opportunity.titlePlural,
                columns: ['title', 'amount', 'stageId', 'expectedClose'],
                conditions: [{ name: 'customerId', operator: { type: 'equal' }, value: '{{ context.eval("id") }}' }],
                actions: [
                    {
                        title: '@general:actions.create.title',
                        command: { name: 'create-entity', options: { process: { redirect: false, canCreateNewOne: true } } },
                        priority: 'primary',
                        type: 'create',
                        scope: AXPEntityCommandScope.TypeLevel,
                    },
                ],
                layout: {
                    type: 'page-list',
                },
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.activity.name}`,
                title: RootConfig.entities.activity.titlePlural,
                columns: ['type', 'subject', 'dueDate', 'status'],
                conditions: [{ name: 'customerId', operator: { type: 'equal' }, value: '{{ context.eval("id") }}' }],
                actions: [
                    {
                        title: '@general:actions.create.title',
                        command: { name: 'create-entity', options: { process: { redirect: false, canCreateNewOne: true } } },
                        priority: 'primary',
                        type: 'create',
                        scope: AXPEntityCommandScope.TypeLevel,
                    },
                ],
                layout: {
                    type: 'page-list',
                },
            },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'customer-profile' }, { id: 'additional-info' }],
                    properties: [
                        { name: 'customerCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'customerTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'lifecycleStageId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'territoryId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isVip', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'accountManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'customer-profile' }, { id: 'additional-info' }],
                    properties: [
                        { name: 'customerCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'customerTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'lifecycleStageId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'territoryId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isVip', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'accountManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                single: {
                    title: '{{ person.displayName }}',
                    sections: [
                        { id: 'basic-info', order: 1, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'customer-profile', order: 2, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'additional-info', order: 3, layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'customerCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'customerTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'lifecycleStageId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'territoryId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isVip', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'accountManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [
                        createAllQueryView({
                            sorts: [{ name: 'person.displayName', dir: 'asc' }],
                        }),
                    ],
                },
            },
        },
    };
    return entityDef;
}

export { customerFactory };
//# sourceMappingURL=acorex-modules-customer-management-customer.entity-BOUGKl2b.mjs.map
