import { AXTranslationService } from '@acorex/core/translation';
import { RootConfig } from '@acorex/modules/contact-management';
import { createAllQueryView, createQueryView, AXPEntityQueryType } from '@acorex/platform/common';
import { AXPWidgetsCatalog } from '@acorex/platform/layout/widget-core';
import { entityMasterCrudActions, entityMasterRecordActions, entityDetailsReferenceCreateActions, entityDetailsReferenceCondition, entityDetailsCrudActions, entityDetailsSimpleCondition } from '@acorex/platform/layout/entity';
import { a as AXMPartyService, R as RootConfig$1 } from './acorex-modules-party-management-acorex-modules-party-management-BkRsPvsw.mjs';

//#region ----   Imports   ----
//#endregion
async function partyFactory(injector) {
    const dataService = injector.get(AXMPartyService);
    const translateService = injector.get(AXTranslationService);
    // Resolve texts for select option labels
    // Using static keys ensures i18n pipes/services render properly
    const partyTypeIndividualText = await translateService.translateAsync('@party-management:party.properties.type.options.individual');
    const partyTypeOrganizationText = await translateService.translateAsync('@party-management:party.properties.type.options.organization');
    const entityDef = {
        module: RootConfig$1.module.name,
        name: RootConfig$1.entities.party.name,
        title: RootConfig$1.entities.party.title,
        icon: 'fa-light fa-user',
        formats: {
            individual: RootConfig$1.entities.party.title,
            displayName: 'Information',
            plural: RootConfig$1.entities.party.titlePlural,
            searchResult: {
                title: '{{ displayName }}',
                description: '@party-management:party.search-result-description',
            },
        },
        plugins: [
            {
                name: 'extra-properties',
            },
            // {
            //   name: 'computed-property',
            //   options: {
            //     fields: [
            //       {
            //         name: 'primaryContact',
            //         title: `Primary Contact`,
            //         group: {
            //           id: 'computed-properties',
            //           title: 'Primaries',
            //         },
            //         showAs: {
            //           type: AXPWidgetsCatalog.contact,
            //           options: {
            //             type: "email",
            //             width: "300px",
            //             hasLabel: false,
            //             disabled: true,
            //           },
            //         },
            //         conditions: [
            //           {
            //             name: 'isPrimary',
            //             operator: { type: 'equal' },
            //             value: true,
            //           },
            //         ],
            //         value: '{{ context.eval("contacts.{value}")?.[0]?.value}}',
            //       },
            //     ],
            //   },
            // },
        ],
        groups: [
            {
                id: 'basic-info',
                title: '@general:terms.interface.basic-info',
            },
        ],
        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',
                    readonly: true,
                    interface: {
                        type: AXPWidgetsCatalog.select,
                        options: {
                            valueField: 'id',
                            textField: 'title',
                            dataSource: [
                                { id: 'individual', title: partyTypeIndividualText },
                                { id: 'organization', title: partyTypeOrganizationText },
                            ],
                            expose: [
                                { source: 'id', target: 'type.id' },
                                { source: 'title', target: 'type.title' },
                            ],
                        },
                    },
                },
                validations: [
                    {
                        rule: 'required',
                    },
                ],
            },
            {
                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: AXPWidgetsCatalog.text,
                    },
                },
                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: AXPWidgetsCatalog.text,
                    },
                    hidden: '{{ context.eval("type.id") == "organization"  }}',
                },
            },
            {
                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: AXPWidgetsCatalog.text,
                    },
                    hidden: '{{ context.eval("type.id") == "organization"  }}',
                },
            },
            {
                name: 'legalName',
                title: '@party-management:party.properties.legal-name.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsCatalog.text,
                    },
                    hidden: '{{ context.eval("type.id") == "individual"  }}',
                },
            },
        ],
        columns: [
            { name: 'displayName', options: { width: '300px' } },
            { name: 'typeId', options: { dataPath: 'type.title' } },
            { name: 'legalName', options: { width: '300px' } },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.contactMethod.name}`,
                title: '@party-management:party.related-entities.contacts.title',
                columns: ['methodTypeId', 'value', 'isPrimary', 'isVerified', 'notes'],
                conditions: [...entityDetailsReferenceCondition(RootConfig$1.entities.party.fullname)],
                actions: [...entityDetailsReferenceCreateActions(RootConfig$1.entities.party.fullname)],
                layout: { type: 'tab-list' },
                persistence: {
                    dataPath: 'contacts',
                    strategy: 'referenced',
                },
            },
            {
                entity: 'LocationManagement.Location',
                title: '@party-management:party.related-entities.locations.title',
                columns: [
                    'title',
                    'locationTypeId',
                    'street1',
                    'cityId',
                    'stateId',
                    'countryId',
                    'postalCode',
                    'isPrimary',
                    'isActive',
                ],
                conditions: [...entityDetailsReferenceCondition(RootConfig$1.entities.party.fullname)],
                actions: [...entityDetailsReferenceCreateActions(RootConfig$1.entities.party.fullname)],
                layout: { type: 'tab-list' },
                persistence: {
                    dataPath: 'locations',
                    strategy: 'referenced',
                },
            },
            {
                entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.partyRole.name}`,
                title: '@party-management:party.related-entities.roles.title',
                columns: ['roleId', 'status', 'since', 'until', 'notes'],
                conditions: [entityDetailsSimpleCondition('partyId')],
                actions: [...entityDetailsCrudActions('partyId')],
                layout: { type: 'page-list' },
            },
            {
                entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.partyIdentifier.name}`,
                title: '@party-management:party.related-entities.identifiers.title',
                columns: ['typeId', 'value', 'countryId', 'isPrimary', 'validFrom', 'validTo', 'attachments'],
                conditions: [entityDetailsSimpleCondition('partyId')],
                actions: [...entityDetailsCrudActions('partyId')],
                layout: { type: 'tab-list' },
            },
            {
                entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.partyRelationship.name}`,
                title: '@party-management:party.related-entities.relationships.title',
                columns: ['toPartyId', 'typeId', 'validFrom', 'validTo', 'notes'],
                conditions: [entityDetailsSimpleCondition('fromPartyId')],
                actions: [...entityDetailsCrudActions('fromPartyId')],
                layout: { type: 'page-list' },
            },
        ],
        commands: {
            create: {
                execute: async (data) => {
                    const res = await dataService.insertOne(data);
                    return { id: res };
                },
            },
            delete: {
                execute: async (id) => {
                    return await dataService.deleteOne(id);
                },
            },
            update: {
                execute: async (data) => {
                    return await dataService.updateOne(data.id, data);
                },
            },
        },
        queries: {
            byKey: {
                execute: async (id) => {
                    return await dataService.getOne(id);
                },
                type: AXPEntityQueryType.Single,
            },
            list: {
                execute: async (e) => {
                    return await dataService.query(e);
                },
                type: AXPEntityQueryType.List,
            },
        },
        interfaces: {
            master: {
                create: {
                    sections: [
                        {
                            id: 'basic-info',
                            order: 1,
                        },
                    ],
                    properties: [
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 2,
                                    },
                                },
                            },
                        },
                        {
                            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: 'notes',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 8,
                                    },
                                },
                            },
                        },
                    ],
                },
                update: {
                    sections: [
                        {
                            id: 'basic-info',
                        },
                    ],
                    properties: [
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 2,
                                    },
                                },
                            },
                        },
                        {
                            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: 'notes',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 8,
                                    },
                                },
                            },
                        },
                    ],
                },
                single: {
                    title: '{{displayName}}',
                    sections: [
                        {
                            id: 'basic-info',
                        },
                    ],
                    properties: [
                        {
                            name: 'typeId',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 1,
                                    },
                                },
                            },
                        },
                        {
                            name: 'displayName',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 6,
                                        order: 2,
                                    },
                                },
                            },
                        },
                        {
                            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: 'notes',
                            layout: {
                                positions: {
                                    lg: {
                                        colSpan: 12,
                                        order: 8,
                                    },
                                },
                            },
                        },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [
                        createAllQueryView({
                            sorts: [{ name: 'displayName', dir: 'asc' }],
                        }),
                        createQueryView('individuals', partyTypeIndividualText, true, {
                            conditions: [
                                {
                                    name: 'type.id',
                                    operator: { type: 'equal' },
                                    value: 'individual',
                                },
                            ],
                        }),
                        createQueryView('organizations', partyTypeOrganizationText, true, {
                            conditions: [
                                {
                                    name: 'type.id',
                                    operator: { type: 'equal' },
                                    value: 'organization',
                                },
                            ],
                        }),
                    ],
                },
            },
        },
    };
    return entityDef;
}

export { partyFactory };
//# sourceMappingURL=acorex-modules-party-management-party.entity-Dw-VX8US.mjs.map
