import { AXPWidgetsList } from '@acorex/modules/common';
import { createAllQueryView } from '@acorex/platform/common';
import { AXPWidgetsCatalog } from '@acorex/platform/layout/widget-core';
import { entityDetailsCrudActions } from '@acorex/platform/layout/entity';
import { RootConfig } from './acorex-modules-order-management.mjs';

//#region ----   Imports   ----
//#endregion
//#region ----   Factory Function   ----
async function memberOrderLineFactory(injector) {
    const i18n = `${RootConfig.config.i18n}:bulk-buy-orders.entities.member-order-line`;
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.memberOrderLine.name,
        title: RootConfig.entities.memberOrderLine.title,
        icon: RootConfig.entities.memberOrderLine.icon,
        formats: {
            individual: RootConfig.entities.memberOrderLine.title,
            plural: RootConfig.entities.memberOrderLine.titlePlural,
            displayName: RootConfig.entities.memberOrderLine.title,
            searchResult: {
                title: '{{ bulkBuyOrderItem.product.title }}',
                description: '{{ quantity }} × {{ price }}',
            },
        },
        plugins: [
            {
                name: 'computed-property',
                options: {
                    fields: [
                        {
                            name: 'lineTotal',
                            title: `@${i18n}.fields.line-total.title`,
                            value: '{{ context.eval("price") && context.eval("quantity") ? (context.eval("price") * context.eval("quantity") * (1 + (context.eval("taxRate") || 0) / 100)).toFixed(2) : 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: 'product-info', title: `@${i18n}.groups.product-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: 'memberOrderId',
                title: `@${i18n}.fields.member-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.memberOrder.name}`,
                            textField: 'member.displayName',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'memberOrder.id' },
                                { source: 'member.displayName', target: 'memberOrder.member.displayName' },
                                { source: 'bulkBuyOrder.orderNumber', target: 'memberOrder.bulkBuyOrder.orderNumber' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'bulkBuyOrderItemId',
                title: `@${i18n}.fields.bulk-buy-order-item.title`,
                groupId: 'product-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.bulkBuyOrderItem.name}`,
                            textField: 'product.title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'bulkBuyOrderItem.id' },
                                { source: 'product.title', target: 'bulkBuyOrderItem.product.title' },
                                { source: 'price', target: 'bulkBuyOrderItem.price' },
                                { source: 'unit', target: 'bulkBuyOrderItem.unit' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'quantity',
                title: `@${i18n}.fields.quantity.title`,
                groupId: 'product-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'number',
                    interface: {
                        type: AXPWidgetsList.Editors.NumberBox,
                        options: {
                            minValue: 0,
                            decimals: 2,
                        },
                    },
                },
                validations: [
                    { rule: 'required' },
                    {
                        rule: 'minValue',
                        options: {
                            value: 0,
                        },
                    },
                ],
            },
            {
                name: 'price',
                title: `@${i18n}.fields.price.title`,
                groupId: 'financial-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: false } },
                },
                schema: {
                    dataType: 'number',
                    interface: {
                        type: AXPWidgetsList.Editors.NumberBox,
                        options: {
                            minValue: 0,
                            decimals: 2,
                            format: 'currency',
                            readonly: true,
                        },
                    },
                },
                validations: [
                    {
                        rule: 'minValue',
                        options: {
                            value: 0,
                        },
                    },
                ],
            },
            {
                name: 'taxRate',
                title: `@${i18n}.fields.tax-rate.title`,
                groupId: 'financial-info',
                schema: {
                    dataType: 'number',
                    visible: false,
                    interface: {
                        type: AXPWidgetsCatalog.number,
                    },
                },
            },
        ],
        columns: [
            { name: 'memberOrderId', options: { dataPath: 'memberOrder.member.displayName' } },
            { name: 'bulkBuyOrderItemId', options: { dataPath: 'bulkBuyOrderItem.product.title' } },
            { name: 'quantity' },
            { name: 'price' },
            { name: 'lineTotal' },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'order-info' }, { id: 'product-info' }, { id: 'financial-info' }],
                    properties: [
                        { name: 'memberOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'bulkBuyOrderItemId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'quantity', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'order-info' }, { id: 'product-info' }, { id: 'financial-info' }],
                    properties: [
                        { name: 'memberOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'bulkBuyOrderItemId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'quantity', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                single: {
                    title: '{{ bulkBuyOrderItem.product.title }}',
                    sections: [
                        { id: 'order-info', order: 1, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'product-info', order: 2, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'financial-info', order: 3, layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'memberOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'bulkBuyOrderItemId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'quantity', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'price', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'lineTotal', layout: { positions: { lg: { colSpan: 4 } } } },
                    ],
                },
                list: {
                    actions: [...entityDetailsCrudActions('memberOrderId')],
                    views: [createAllQueryView()],
                },
            },
        },
    };
    return entityDef;
}
//#endregion

export { memberOrderLineFactory };
//# sourceMappingURL=acorex-modules-order-management-member-order-line.entity-C-55ZARU.mjs.map
