import { AXTranslationService } from '@acorex/core/translation';
import { AXPWidgetsList } from '@acorex/modules/common';
import { createAllQueryView, createQueryView } from '@acorex/platform/common';
import { entityMasterCrudActions, entityMasterRecordActions, entityDetailsCrudActions, entityDetailsSimpleCondition } from '@acorex/platform/layout/entity';
import { RootConfig } from './acorex-modules-person-management.mjs';

//#region ----   Imports   ----
//#endregion
async function personFactory(injector) {
    const translateService = injector.get(AXTranslationService);
    // Resolve texts for select option labels
    // Using static keys ensures i18n pipes/services render properly
    const personTypeIndividualText = await translateService.translateAsync('@person-management:person.properties.type.options.natural');
    const personTypeOrganizationText = await translateService.translateAsync('@person-management:person.properties.type.options.legal');
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.person.name,
        title: RootConfig.entities.person.title,
        icon: 'fa-light fa-user',
        formats: {
            individual: RootConfig.entities.person.title,
            displayName: 'Information',
            plural: RootConfig.entities.person.titlePlural,
            searchResult: {
                title: '{{ displayName }}',
                description: '@person-management:person.search-result-description',
            },
        },
        plugins: [{ name: 'history' }, { name: 'archive' }, { name: 'compare' }],
        groups: [
            {
                id: 'basic-info',
                title: '@general:terms.interface.basic-info',
                icon: 'fa-light fa-info',
            },
            {
                id: 'phones',
                title: '@person-management:person.properties.phones.title',
                icon: 'fa-light fa-phone',
            },
            {
                id: 'emails',
                title: '@person-management:person.properties.emails.title',
                icon: 'fa-light fa-envelope',
            },
            {
                id: 'social',
                title: '@person-management:person.properties.social.title',
                icon: 'fa-light fa-globe',
            },
            {
                id: 'addresses',
                title: '@person-management:person.related-entities.locations.title',
                icon: 'fa-light fa-location-dot',
            },
        ],
        properties: [
            {
                name: 'typeId',
                title: '@general:terms.common.type',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.SelectBox,
                        options: {
                            valueField: 'id',
                            textField: 'title',
                            dataSource: [
                                { id: 'natural', title: personTypeIndividualText },
                                { id: 'legal', title: personTypeOrganizationText },
                            ],
                            expose: [
                                { source: 'id', target: 'type.id' },
                                { source: 'title', target: 'type.title' },
                            ],
                        },
                    },
                },
                validations: [
                    {
                        rule: 'required',
                    },
                ],
            },
            {
                name: 'image',
                title: '@person-management:person.properties.image.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Advanced.Image,
                        options: {
                            type: 'avatar',
                        },
                    },
                },
            },
            {
                name: 'displayName',
                title: '@general:terms.common.display-name',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.TextBox,
                    },
                },
                validations: [
                    {
                        rule: 'required',
                    },
                ],
            },
            {
                name: 'firstName',
                title: '@general:terms.contact.first-name',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.TextBox,
                    },
                    visible: '{{ context.eval("type.id") == "natural"  }}',
                },
            },
            {
                name: 'lastName',
                title: '@general:terms.contact.last-name',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.TextBox,
                    },
                    visible: '{{ context.eval("type.id") == "natural"  }}',
                },
            },
            {
                name: 'legalName',
                title: '@person-management:person.properties.legal-name.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.TextBox,
                    },
                    visible: '{{ context.eval("type.id") != "natural"  }}',
                },
            },
            {
                name: 'phones',
                title: '@person-management:person.properties.phones.title',
                groupId: 'phones',
                schema: {
                    dataType: 'object',
                    defaultValue: [],
                    interface: {
                        type: AXPWidgetsList.Layouts.RepeaterLayout,
                        options: {
                            hasControls: true,
                        },
                        children: [
                            {
                                type: AXPWidgetsList.Editors.ContactBox,
                                mode: 'edit',
                                path: '',
                                options: {
                                    type: 'phone',
                                    hasLabel: true,
                                    hasIcon: true,
                                    placeholder: 'Enter phone number',
                                    validations: [
                                        {
                                            rule: 'required',
                                        },
                                    ],
                                },
                            },
                        ],
                    },
                },
            },
            {
                name: 'emails',
                title: '@person-management:person.properties.emails.title',
                groupId: 'emails',
                schema: {
                    dataType: 'object',
                    defaultValue: [],
                    interface: {
                        type: AXPWidgetsList.Layouts.RepeaterLayout,
                        options: {
                            hasControls: true,
                            isDraggable: true,
                        },
                        children: [
                            {
                                type: AXPWidgetsList.Editors.ContactBox,
                                mode: 'edit',
                                path: '',
                                options: {
                                    type: 'email',
                                    hasLabel: true,
                                    hasIcon: true,
                                    placeholder: 'Enter email address',
                                    validations: [
                                        {
                                            rule: 'required',
                                        },
                                    ],
                                },
                            },
                        ],
                    },
                },
            },
            {
                name: 'social',
                title: '@person-management:person.properties.social.title',
                groupId: 'social',
                schema: {
                    dataType: 'object',
                    defaultValue: [],
                    interface: {
                        type: AXPWidgetsList.Layouts.RepeaterLayout,
                        options: {
                            hasControls: true,
                            isDraggable: true,
                        },
                        children: [
                            {
                                type: AXPWidgetsList.Editors.ContactBox,
                                mode: 'edit',
                                path: '',
                                options: {
                                    type: 'url',
                                    hasLabel: true,
                                    hasIcon: true,
                                    placeholder: 'Enter social media link',
                                    validations: [
                                        {
                                            rule: 'required',
                                        },
                                    ],
                                },
                            },
                        ],
                    },
                },
            },
            {
                name: 'addresses',
                title: '@person-management:person.related-entities.locations.title',
                groupId: 'addresses',
                schema: {
                    dataType: 'object',
                    defaultValue: [],
                    interface: {
                        type: AXPWidgetsList.Layouts.RepeaterLayout,
                        options: {
                            hasControls: true,
                            isDraggable: true,
                        },
                        children: [
                            {
                                type: AXPWidgetsList.Editors.AddressBox,
                                mode: 'edit',
                                path: '',
                                options: {
                                    mode: 'advanced',
                                    hasLabel: true,
                                    validations: [
                                        {
                                            rule: 'required',
                                        },
                                    ],
                                },
                            },
                        ],
                    },
                },
            },
        ],
        columns: [
            { name: 'displayName', options: { width: '300px' } },
            { name: 'typeId', options: { dataPath: 'type.title' } },
            { name: 'phones' },
            { name: 'emails' },
            { name: 'social' },
            { name: 'addresses' },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.personRole.name}`,
                title: '@person-management:person.related-entities.roles.title',
                columns: ['roleId', 'status', 'since', 'until', 'notes'],
                conditions: [entityDetailsSimpleCondition('personId')],
                actions: [...entityDetailsCrudActions('personId')],
                layout: { type: 'tab-list' },
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.personIdentifier.name}`,
                title: '@person-management:person.related-entities.identifiers.title',
                columns: ['typeId', 'value', 'countryId', 'isPrimary', 'validFrom', 'validTo', 'attachments'],
                conditions: [entityDetailsSimpleCondition('personId')],
                actions: [...entityDetailsCrudActions('personId')],
                layout: { type: 'page-list' },
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.personRelationship.name}`,
                title: '@person-management:person.related-entities.relationships.title',
                columns: ['toPersonId', 'typeId', 'validFrom', 'validTo', 'notes'],
                conditions: [entityDetailsSimpleCondition('fromPersonId')],
                actions: [...entityDetailsCrudActions('fromPersonId')],
                layout: { type: 'page-list' },
            },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [
                        {
                            id: 'basic-info',
                            order: 1,
                        },
                        {
                            id: 'phones',
                            order: 2,
                        },
                        {
                            id: 'emails',
                            order: 3,
                        },
                        {
                            id: 'social',
                            order: 4,
                        },
                        {
                            id: 'addresses',
                            order: 5,
                        },
                    ],
                    properties: [
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'firstName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 3,
                                    },
                                },
                            },
                        },
                        {
                            name: 'lastName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 4,
                                    },
                                },
                            },
                        },
                        {
                            name: 'legalName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 5,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 6,
                                    },
                                },
                            },
                        },
                        {
                            name: 'phones',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 9,
                                    },
                                },
                            },
                        },
                        {
                            name: 'emails',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 10,
                                    },
                                },
                            },
                        },
                        {
                            name: 'social',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 11,
                                    },
                                },
                            },
                        },
                        {
                            name: 'addresses',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 12,
                                    },
                                },
                            },
                        },
                    ],
                },
                update: {
                    sections: [
                        {
                            id: 'basic-info',
                        },
                        {
                            id: 'phones',
                        },
                        {
                            id: 'emails',
                        },
                        {
                            id: 'social',
                        },
                        {
                            id: 'addresses',
                        },
                    ],
                    properties: [
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'firstName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 3,
                                    },
                                },
                            },
                        },
                        {
                            name: 'lastName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 4,
                                    },
                                },
                            },
                        },
                        {
                            name: 'legalName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 5,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 6,
                                    },
                                },
                            },
                        },
                        {
                            name: 'phones',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 9,
                                    },
                                },
                            },
                        },
                        {
                            name: 'emails',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 10,
                                    },
                                },
                            },
                        },
                        {
                            name: 'social',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 11,
                                    },
                                },
                            },
                        },
                        {
                            name: 'addresses',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 12,
                                    },
                                },
                            },
                        },
                    ],
                },
                single: {
                    title: '{{displayName}}',
                    sections: [
                        {
                            id: 'basic-info',
                        },
                        {
                            id: 'phones',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                    },
                                },
                            },
                        },
                        {
                            id: 'emails',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        colStart: 7,
                                    },
                                },
                            },
                        },
                        {
                            id: 'social',
                        },
                        {
                            id: 'addresses',
                        },
                    ],
                    properties: [
                        {
                            name: 'image',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        rowSpan: 4,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 2,
                                    },
                                },
                            },
                        },
                        {
                            name: 'firstName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 4,
                                    },
                                },
                            },
                        },
                        {
                            name: 'lastName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 3,
                                        order: 5,
                                    },
                                },
                            },
                        },
                        {
                            name: 'legalName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 6,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 7,
                                    },
                                },
                            },
                        },
                        {
                            name: 'phones',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 8,
                                    },
                                },
                            },
                        },
                        {
                            name: 'emails',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 9,
                                    },
                                },
                            },
                        },
                        {
                            name: 'social',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 10,
                                    },
                                },
                            },
                        },
                        {
                            name: 'addresses',
                            layout: {
                                label: {
                                    visible: false,
                                },
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 11,
                                    },
                                },
                            },
                        },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [
                        createAllQueryView({
                            sorts: [{ name: 'displayName', dir: 'asc' }],
                        }),
                        createQueryView('naturals', personTypeIndividualText, true, {
                            conditions: [
                                {
                                    name: 'type.id',
                                    operator: { type: 'equal' },
                                    value: 'natural',
                                },
                            ],
                        }),
                        createQueryView('legals', personTypeOrganizationText, true, {
                            conditions: [
                                {
                                    name: 'type.id',
                                    operator: { type: 'equal' },
                                    value: 'legal',
                                },
                            ],
                        }),
                    ],
                },
            },
        },
    };
    return entityDef;
}

export { personFactory };
//# sourceMappingURL=acorex-modules-person-management-person.entity-CaEVw1mL.mjs.map
