import { AXPWidgetsList } from '@acorex/modules/common';
import { RootConfig as RootConfig$1 } from '@acorex/modules/product-catalog';
import { RootConfig as RootConfig$2 } from '@acorex/modules/measurement-core';
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 bulkBuyOrderItemFactory(injector) {
    const i18n = `${RootConfig.config.i18n}:bulk-buy-orders.entities.bulk-buy-order-item`;
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.bulkBuyOrderItem.name,
        title: RootConfig.entities.bulkBuyOrderItem.title,
        icon: RootConfig.entities.bulkBuyOrderItem.icon,
        formats: {
            individual: RootConfig.entities.bulkBuyOrderItem.title,
            plural: RootConfig.entities.bulkBuyOrderItem.titlePlural,
            displayName: RootConfig.entities.bulkBuyOrderItem.title,
            searchResult: {
                title: '{{ product.title }}',
                description: '{{ price }} per {{ unit.title }}',
            },
        },
        plugins: [
            {
                name: 'computed-property',
                options: {
                    fields: [
                        {
                            name: 'total',
                            title: `@${i18n}.fields.total.title`,
                            value: '{{ context.eval("price") && context.eval("quantityInOrder") ? (context.eval("price") * context.eval("quantityInOrder") * (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',
                                },
                            },
                        },
                        {
                            name: 'perUnitInclGst',
                            title: `@${i18n}.fields.per-unit-incl-gst.title`,
                            value: '{{ context.eval("total") && context.eval("size") && context.eval("size") > 0 ? (context.eval("total") / context.eval("size")).toFixed(2) : 0 }}',
                            group: {
                                id: 'financial-info',
                                title: `@${i18n}.groups.financial-info`,
                            },
                            showAs: {
                                type: AXPWidgetsCatalog.number,
                                options: {
                                    readonly: true,
                                    format: 'currency',
                                },
                            },
                        },
                        {
                            name: 'quantityAvailable',
                            title: `@${i18n}.fields.quantity-available.title`,
                            value: '{{ context.eval("size") && context.eval("quantityInOrder") ? (context.eval("size") - context.eval("quantityInOrder")) : (context.eval("size") || 0) }}',
                            group: {
                                id: 'order-info',
                                title: `@${i18n}.groups.order-info`,
                            },
                            showAs: {
                                type: AXPWidgetsCatalog.number,
                                options: {
                                    readonly: true,
                                },
                            },
                        },
                    ],
                },
            },
        ],
        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: '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' },
                                { source: 'supplierId', target: 'bulkBuyOrder.supplierId' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'productId',
                title: `@${i18n}.fields.product.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$1.module.name}.${RootConfig$1.entities.product.name}`,
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'product.id' },
                                { source: 'title', target: 'product.title' },
                                { source: 'code', target: 'product.code' },
                            ],
                            filter: {
                                logic: 'and',
                                filters: [
                                    {
                                        field: 'supplierProducts.supplierId',
                                        operator: { type: 'equal' },
                                        value: '{{ context.eval("bulkBuyOrder.supplierId") }}',
                                    },
                                ],
                            },
                            // triggers: [
                            //     {
                            //         event: "{{ events.context('bulkBuyOrder.supplierId') }}",
                            //         action: `{{ widget.refresh() }}`,
                            //     },
                            // ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'unit',
                title: `@${i18n}.fields.unit.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$2.module.name}.${RootConfig$2.entities.unitOfMeasure.name}`,
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'unit.id' },
                                { source: 'title', target: 'unit.title' },
                                { source: 'code', target: 'unit.code' },
                                { source: 'symbol', target: 'unit.symbol' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'size',
                title: `@${i18n}.fields.size.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: 'quantityInOrder',
                title: `@${i18n}.fields.quantity-in-order.title`,
                groupId: 'order-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,
                            readonly: true,
                        },
                    },
                },
                validations: [
                    {
                        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: true } },
                },
                schema: {
                    dataType: 'number',
                    interface: {
                        type: AXPWidgetsList.Editors.NumberBox,
                        options: {
                            minValue: 0,
                            decimals: 2,
                            format: 'currency',
                        },
                    },
                },
                validations: [
                    { rule: 'required' },
                    {
                        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: 'productId', options: { dataPath: 'product.title' } },
            { name: 'size' },
            { name: 'quantityInOrder' },
            { name: 'quantityAvailable' },
            { name: 'price' },
            { name: 'unit', options: { dataPath: 'unit.title' } },
            { name: 'total' },
            { name: 'perUnitInclGst' },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'order-info' }, { id: 'product-info' }, { id: 'financial-info' }],
                    properties: [
                        //{ name: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'productId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'unit', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'size', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'price', layout: { positions: { lg: { colSpan: 6, colStart: 1 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'order-info' }, { id: 'product-info' }, { id: 'financial-info' }],
                    properties: [
                        //{ name: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'productId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'unit', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'size', layout: { positions: { lg: { colSpan: 6, colStart: 1 } } } },
                        { name: 'price', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                single: {
                    title: '{{ 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: 'bulkBuyOrderId', layout: { positions: { lg: { colSpan: 12 } } } },
                        { name: 'productId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'unit', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'size', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'quantityInOrder', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'quantityAvailable', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'price', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'total', layout: { positions: { lg: { colSpan: 4 } } } },
                        { name: 'perUnitInclGst', layout: { positions: { lg: { colSpan: 4 } } } },
                    ],
                },
                list: {
                    actions: [...entityDetailsCrudActions('bulkBuyOrderId')],
                    views: [createAllQueryView()],
                },
            },
        },
    };
    return entityDef;
}
//#endregion

export { bulkBuyOrderItemFactory };
//# sourceMappingURL=acorex-modules-order-management-bulk-buy-order-item.entity-Cqsc1tku.mjs.map
