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

//#region ----   Imports   ----
//#endregion
//#region ----   Factory Function   ----
async function memberOrderFactory(injector) {
    const i18n = `${RootConfig.config.i18n}:bulk-buy-orders.entities.member-order`;
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.memberOrder.name,
        title: RootConfig.entities.memberOrder.title,
        icon: RootConfig.entities.memberOrder.icon,
        formats: {
            individual: RootConfig.entities.memberOrder.title,
            plural: RootConfig.entities.memberOrder.titlePlural,
            displayName: 'Member Order',
            searchResult: {
                title: '{{ member.displayName }}',
                description: '{{ totalOwing }}',
            },
        },
        plugins: [
            { name: 'history' },
            { name: 'lock' },
            { name: 'notes' },
            {
                name: 'status',
                options: {
                    field: 'statusId',
                    definition: 'order-management.status.member-order',
                },
            },
            {
                name: 'computed-property',
                options: {
                    fields: [
                        {
                            name: 'productTotal',
                            title: `@${i18n}.fields.product-total.title`,
                            value: '{{ context.eval("productTotal") || 0 }}',
                            group: {
                                id: 'financial-info',
                                title: `@${i18n}.groups.financial-info`,
                            },
                            showAs: {
                                type: AXPWidgetsCatalog.number,
                                options: {
                                    readonly: true,
                                    format: 'currency',
                                },
                            },
                        },
                        {
                            name: 'freightShare',
                            title: `@${i18n}.fields.freight-share.title`,
                            value: '{{ context.eval("freightShare") || 0 }}',
                            group: {
                                id: 'financial-info',
                                title: `@${i18n}.groups.financial-info`,
                            },
                            showAs: {
                                type: AXPWidgetsCatalog.number,
                                options: {
                                    readonly: true,
                                    format: 'currency',
                                },
                            },
                        },
                        {
                            name: 'totalOwing',
                            title: `@${i18n}.fields.total-owing.title`,
                            value: '{{ (context.eval("productTotal") || 0) + (context.eval("freightShare") || 0) }}',
                            group: {
                                id: 'financial-info',
                                title: `@${i18n}.groups.financial-info`,
                            },
                            showAs: {
                                type: AXPWidgetsCatalog.number,
                                options: {
                                    readonly: true,
                                    format: 'currency',
                                },
                            },
                        },
                    ],
                },
            },
        ],
        groups: [
            { id: 'order-info', title: `@${i18n}.groups.order-info` },
            { id: 'member-info', title: `@${i18n}.groups.member-info` },
            { id: 'financial-info', title: `@${i18n}.groups.financial-info` },
        ],
        properties: [
            {
                name: 'id',
                title: 'ID',
                groupId: 'order-info',
                schema: { dataType: 'uuid', visible: false, nullable: false, readonly: true, unique: { enabled: true } },
            },
            {
                name: 'bulkBuyOrderId',
                title: `@${i18n}.fields.bulk-buy-order.title`,
                groupId: 'order-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig.module.name}.${RootConfig.entities.bulkBuyOrder.name}`,
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'bulkBuyOrder.id' },
                                { source: 'orderNumber', target: 'bulkBuyOrder.orderNumber' },
                                { source: 'title', target: 'bulkBuyOrder.title' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'memberId',
                title: `@${i18n}.fields.member.title`,
                groupId: 'member-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.person.name}`,
                            textField: 'displayName',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'member.id' },
                                { source: 'displayName', target: 'member.displayName' },
                                { source: 'email', target: 'member.email' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'productTotal',
                title: `@${i18n}.fields.product-total.title`,
                groupId: 'financial-info',
                schema: {
                    dataType: 'number',
                    visible: false,
                    interface: {
                        type: AXPWidgetsCatalog.number,
                    },
                },
            },
            {
                name: 'freightShare',
                title: `@${i18n}.fields.freight-share.title`,
                groupId: 'financial-info',
                schema: {
                    dataType: 'number',
                    visible: false,
                    interface: {
                        type: AXPWidgetsCatalog.number,
                    },
                },
            },
            {
                name: 'paidDate',
                title: `@${i18n}.fields.paid-date.title`,
                groupId: 'financial-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: false } },
                },
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsCatalog.dateTime,
                        options: {
                            showTime: false,
                        },
                    },
                },
            },
        ],
        columns: [
            { name: 'memberId', options: { dataPath: 'member.displayName' } },
            { name: 'bulkBuyOrderId', options: { dataPath: 'bulkBuyOrder.orderNumber' } },
            { name: 'productTotal' },
            { name: 'freightShare' },
            { name: 'totalOwing' },
            { name: 'statusId' },
            { name: 'paidDate' },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.memberOrderLine.name}`,
                title: RootConfig.entities.memberOrderLine.titlePlural,
                columns: ['bulkBuyOrderItemId', 'quantity', 'price', 'lineTotal'],
                conditions: [entityDetailsSimpleCondition('memberOrderId')],
                actions: [...entityDetailsCrudActions('memberOrderId')],
                layout: { type: 'tab-list' },
            },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'order-info' }, { id: 'member-info' }, { id: 'financial-info' }],
                    properties: [
                        { name: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'memberId', layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'order-info' }, { id: 'member-info' }, { id: 'financial-info' }],
                    properties: [
                        { name: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'memberId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'paidDate', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                single: {
                    title: '{{ member.displayName }}',
                    sections: [
                        { id: 'order-info', order: 1, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'member-info', order: 2, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'financial-info', order: 3, layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'memberId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'productTotal', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'freightShare', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'totalOwing', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'paidDate', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [
                        createAllQueryView({
                            sorts: [{ name: 'member.displayName', dir: 'asc' }],
                        }),
                    ],
                },
            },
        },
    };
    return entityDef;
}
//#endregion

export { memberOrderFactory };
//# sourceMappingURL=acorex-modules-order-management-member-order.entity-BRAAoKae.mjs.map
