import * as i0 from '@angular/core';
import { Injectable, inject, Injector, NgModule } from '@angular/core';
import { AXP_MODULE_MANIFEST_PROVIDER, AXP_FEATURE_DEFINITION_PROVIDER } from '@acorex/platform/core';
import { AXPEntityService, AXP_ENTITY_DEFINITION_LOADER, entityMasterCrudActions, entityMasterRecordActions, entityDetailsCrudActions, entityDetailsSimpleCondition } from '@acorex/platform/layout/entity';
import { AXP_MENU_PROVIDER, createAllQueryView } from '@acorex/platform/common';
import { AXP_PERMISSION_DEFINITION_PROVIDER } from '@acorex/platform/auth';
import { AXP_IDENTIFIER_RULES } from '@acorex/modules/identifier-management';
import { AXTranslationService } from '@acorex/core/translation';
import { AXPCommonMenuKeys, AXPWidgetsList } from '@acorex/modules/common';
import { RootConfig as RootConfig$1 } from '@acorex/modules/person-management';
import { RootConfig as RootConfig$2 } from '@acorex/modules/financial-core';
import { AXPWidgetsCatalog } from '@acorex/platform/layout/widget-core';

const config = {
    i18n: 'supplier-management',
    module: 'SupplierManagement',
};
const RootConfig = {
    config,
    module: {
        route: 'supplier-management',
        name: config.module,
        title: `@${config.i18n}:module.title`,
        icon: 'fa-light fa-truck',
    },
    entities: {
        supplier: {
            name: 'Supplier',
            source: `${config.module}.Supplier`,
            title: `@${config.i18n}:suppliers.entities.supplier.title`,
            titlePlural: `@${config.i18n}:suppliers.entities.supplier.plural`,
            icon: 'fa-light fa-truck',
        },
    },
};

//#region ----   Imports   ----
//#endregion
//#region ----   Manifest Definition   ----
/**
 * Supplier Management Module Manifest.
 * Defines module metadata, features, and provider references.
 */
const SupplierManagementManifest = {
    name: RootConfig.module.name,
    version: '1.0.0',
    title: RootConfig.module.title,
    icon: RootConfig.module.icon,
    i18n: RootConfig.config.i18n,
    // Module dependencies
    dependencies: ['PersonManagement'],
};
//#endregion

const AXM_SUPPLIER_CODE_IDENTIFIER_RULE = {
    id: 'supplier-code',
    title: 'Supplier Code',
    entity: 'SupplierManagement.Supplier',
    pattern: 'SUPP-{date:YYYYMM}-{seq:4}',
    enabled: true,
    priority: 1,
    scopeConfig: { byTenant: false, period: 'yearly', minValue: 100, blockSize: 0 },
};

//#region ----   Permission Keys   ----
const AXPSupplierManagementPermissionKeys = {
    Suppliers: {
        Management: 'SupplierManagement:Permission:Suppliers.Management',
        View: 'SupplierManagement:Permission:Suppliers.View',
        Create: 'SupplierManagement:Permission:Suppliers.Create',
        Edit: 'SupplierManagement:Permission:Suppliers.Edit',
        Delete: 'SupplierManagement:Permission:Suppliers.Delete',
    },
};
//#endregion

//#region ----   Imports   ----
//#endregion
//#region ----   Permission Definition Provider   ----
class AXMSupplierManagementPermissionDefinitionProvider {
    constructor(injector) {
        this.injector = injector;
    }
    get translateService() {
        return this.injector.get(AXTranslationService);
    }
    async define(context) {
        const trans = async (key) => await this.translateService.translateAsync(`@${RootConfig.config.i18n}:${key}`);
        const keys = AXPSupplierManagementPermissionKeys;
        context
            .addGroup(RootConfig.module.name, await trans('module.title'))
            .addPermission(keys.Suppliers.Management, await trans('suppliers.permissions.suppliers.title'))
            .addChild(keys.Suppliers.View, await trans('suppliers.permissions.suppliers.actions.view.title'))
            .addChild(keys.Suppliers.Create, await trans('suppliers.permissions.suppliers.actions.create.title'))
            .addChild(keys.Suppliers.Edit, await trans('suppliers.permissions.suppliers.actions.edit.title'))
            .addChild(keys.Suppliers.Delete, await trans('suppliers.permissions.suppliers.actions.delete.title'))
            .endPermission()
            .endGroup();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementPermissionDefinitionProvider, deps: [{ token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementPermissionDefinitionProvider }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementPermissionDefinitionProvider, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i0.Injector }] });

const AXPSupplierManagementMenuKeys = {
    Root: 'SupplierManagement:Menu',
    Suppliers: 'SupplierManagement:Menu:Suppliers',
};

class AXMSupplierMenuProvider {
    constructor() {
        this.entityService = inject(AXPEntityService);
    }
    async provide(context) {
        const module = RootConfig.module;
        // Add Suppliers under Contacts menu
        context.find(AXPCommonMenuKeys.Contacts).addItems([
            {
                name: AXPSupplierManagementMenuKeys.Suppliers,
                text: '@supplier-management:suppliers.entities.supplier.plural',
                path: this.entityService.createPath(module.name, RootConfig.entities.supplier.name),
                icon: RootConfig.entities.supplier.icon,
                priority: 3,
                policy: {
                    permissions: [AXPSupplierManagementPermissionKeys.Suppliers.View],
                },
            },
        ]);
    }
}

class AXMSupplierManagementEntityProvider {
    constructor() {
        this.injector = inject(Injector);
    }
    preload() {
        const module = RootConfig.module.name;
        return Array.from(Object.values(RootConfig.entities)).map((entity) => ({
            module: module,
            entity: entity.name,
        }));
    }
    async get(moduleName, entityName) {
        if (moduleName == RootConfig.module.name) {
            switch (entityName) {
                case RootConfig.entities.supplier.name:
                    return (await Promise.resolve().then(function () { return supplier_entity; })).supplierFactory(this.injector);
            }
        }
        return null;
    }
    async list() {
        const m = RootConfig.module.name;
        return Promise.resolve(Object.values(RootConfig.entities).map((e) => ({ name: e.name, module: m })));
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementEntityProvider, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementEntityProvider }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementEntityProvider, decorators: [{
            type: Injectable
        }] });

//#region ----   Imports   ----
//#endregion
//#region ----   Feature Definition Provider   ----
/**
 * Supplier Management Feature Definition Provider.
 * Provides feature definitions for the Supplier Management module.
 */
class AXMSupplierManagementFeatureDefinitionProvider {
    async provide(context) {
        // Management feature removed - module name is used directly as feature key
    }
}
//#endregion

class AXMSupplierManagementModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementModule }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementModule, providers: [
            // Module-specific providers (not part of manifest system)
            {
                provide: AXP_IDENTIFIER_RULES,
                multi: true,
                useValue: AXM_SUPPLIER_CODE_IDENTIFIER_RULE,
            },
            // Module Manifest Provider
            {
                provide: AXP_MODULE_MANIFEST_PROVIDER,
                useValue: SupplierManagementManifest,
                multi: true,
            },
            // Provider references (now using tokens)
            {
                provide: AXP_PERMISSION_DEFINITION_PROVIDER,
                useClass: AXMSupplierManagementPermissionDefinitionProvider,
                multi: true,
            },
            {
                provide: AXP_MENU_PROVIDER,
                useClass: AXMSupplierMenuProvider,
                multi: true,
            },
            {
                provide: AXP_ENTITY_DEFINITION_LOADER,
                useClass: AXMSupplierManagementEntityProvider,
                multi: true,
            },
            {
                provide: AXP_FEATURE_DEFINITION_PROVIDER,
                useClass: AXMSupplierManagementFeatureDefinitionProvider,
                multi: true,
            },
        ] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMSupplierManagementModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [],
                    providers: [
                        // Module-specific providers (not part of manifest system)
                        {
                            provide: AXP_IDENTIFIER_RULES,
                            multi: true,
                            useValue: AXM_SUPPLIER_CODE_IDENTIFIER_RULE,
                        },
                        // Module Manifest Provider
                        {
                            provide: AXP_MODULE_MANIFEST_PROVIDER,
                            useValue: SupplierManagementManifest,
                            multi: true,
                        },
                        // Provider references (now using tokens)
                        {
                            provide: AXP_PERMISSION_DEFINITION_PROVIDER,
                            useClass: AXMSupplierManagementPermissionDefinitionProvider,
                            multi: true,
                        },
                        {
                            provide: AXP_MENU_PROVIDER,
                            useClass: AXMSupplierMenuProvider,
                            multi: true,
                        },
                        {
                            provide: AXP_ENTITY_DEFINITION_LOADER,
                            useClass: AXMSupplierManagementEntityProvider,
                            multi: true,
                        },
                        {
                            provide: AXP_FEATURE_DEFINITION_PROVIDER,
                            useClass: AXMSupplierManagementFeatureDefinitionProvider,
                            multi: true,
                        },
                    ],
                }]
        }] });

async function supplierFactory(injector) {
    const i18n = RootConfig.config.i18n + ':suppliers.entities.supplier';
    const entityName = 'supplier';
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.supplier.name,
        title: RootConfig.entities.supplier.title,
        icon: RootConfig.entities.supplier.icon,
        formats: {
            individual: RootConfig.entities.supplier.title,
            plural: RootConfig.entities.supplier.titlePlural,
            displayName: 'Personal Information',
            searchResult: {
                title: '{{ person.displayName }}',
                description: RootConfig.module.title,
            },
        },
        plugins: [
            { name: 'identifier', options: { field: 'supplierCode', rule: 'supplier-code' } },
            { name: 'history' },
            { name: 'lock' },
            { name: 'compare' },
            {
                name: 'category',
                options: {
                    appearance: { icon: true, color: true },
                    metaData: { enabled: true }
                }
            },
            { name: 'archive' },
            { name: 'notes' },
        ],
        groups: [
            { id: 'basic-info', title: `@${i18n}.groups.basic-info` },
            { id: 'supplier-profile', title: `@${i18n}.groups.supplier-profile` },
            { id: 'financial-info', title: `@${i18n}.groups.financial-info` },
            { id: 'additional-info', title: '@general:terms.interface.additional-info' },
        ],
        properties: [
            {
                name: 'id',
                title: 'ID',
                groupId: 'basic-info',
                schema: { dataType: 'uuid', visible: false, nullable: false, readonly: true, unique: { enabled: true } },
            },
            {
                name: 'supplierCode',
                title: `@${i18n}.fields.supplier-code.title`,
                groupId: 'supplier-profile',
                schema: { dataType: 'string', interface: { type: AXPWidgetsCatalog.text } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'supplierTypeId',
                title: `@${i18n}.fields.supplier-type.title`,
                groupId: 'supplier-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'BusinessCore.PartyType',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'partyType.id' },
                                { source: 'title', target: 'partyType.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'segmentId',
                title: `@${i18n}.fields.segment.title`,
                groupId: 'supplier-profile',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'BusinessCore.PartySegment',
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'partySegment.id' },
                                { source: 'title', target: 'partySegment.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'since',
                title: `@${i18n}.fields.since.title`,
                groupId: 'supplier-profile',
                schema: { dataType: 'date', interface: { type: AXPWidgetsCatalog.dateTime } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'isPreferred',
                title: `@${i18n}.fields.is-preferred.title`,
                groupId: 'supplier-profile',
                schema: { dataType: 'boolean', interface: { type: AXPWidgetsCatalog.toggle } },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: true } } },
            },
            {
                name: 'rating',
                title: `@${i18n}.fields.rating.title`,
                groupId: 'supplier-profile',
                schema: {
                    dataType: 'number',
                    interface: {
                        type: AXPWidgetsList.Editors.RatePicker,
                        options: {
                            min: 0,
                            max: 5,
                        },
                    },
                },
                options: { sort: { enabled: true }, filter: { advance: { enabled: true }, inline: { enabled: false } } },
            },
            {
                name: 'paymentTermsId',
                title: `@${i18n}.fields.payment-terms.title`,
                groupId: 'financial-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig$2.module.name}.${RootConfig$2.entities.paymentTerm.name}`,
                            textField: 'title',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'paymentTerms.id' },
                                { source: 'title', target: 'paymentTerms.title' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: false }, filter: { advance: { enabled: true }, inline: { enabled: false } } },
            },
            {
                name: 'defaultCurrencyId',
                title: `@${i18n}.fields.default-currency.title`,
                groupId: 'financial-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig$2.module.name}.${RootConfig$2.entities.currency.name}`,
                            textField: 'code',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'defaultCurrency.id' },
                                { source: 'code', target: 'defaultCurrency.code' },
                                { source: 'symbol', target: 'defaultCurrency.symbol' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: false }, filter: { advance: { enabled: true }, inline: { enabled: false } } },
            },
            {
                name: 'purchasingManagerId',
                title: `@${i18n}.fields.purchasing-manager.title`,
                groupId: 'additional-info',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `HumanCapitalManagement.Employee`,
                            textField: 'person.displayName',
                            valueField: 'id',
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'purchasingManager.id' },
                                { source: 'employeeCode', target: 'purchasingManager.employeeCode' },
                                { source: 'person.displayName', target: 'purchasingManager.displayName' },
                            ],
                        },
                    },
                },
                options: { sort: { enabled: false }, filter: { advance: { enabled: true }, inline: { enabled: false } } },
            },
        ],
        columns: [
            {
                name: 'person.displayName',
                title: '@general:terms.common.title',
                options: { width: '300px' },
                showAs: { type: AXPWidgetsCatalog.text },
            },
            { name: 'supplierCode' },
            { name: 'supplierTypeId', options: { dataPath: 'partyType.title' } },
            { name: 'segmentId', options: { dataPath: 'partySegment.title' } },
            { name: 'since' },
            { name: 'isPreferred' },
            { name: 'rating' },
            { name: 'purchasingManagerId', options: { dataPath: 'purchasingManager.person.displayName' } },
        ],
        relatedEntities: [
            {
                entity: `${RootConfig$1.module.name}.${RootConfig$1.entities.person.name}`,
                conditions: [
                    {
                        name: 'id',
                        operator: { type: 'equal' },
                        value: '{{ context.eval("personId") }}',
                    },
                ],
                layout: { type: 'merge-detail', position: 'after' },
                persistence: {
                    dataPath: 'person',
                    strategy: 'referenced',
                    idField: 'personId',
                },
            },
            {
                entity: `ProcurementManagement.SupplierProduct`,
                title: '@procurement-management:supplier-products.entities.supplier-product.plural',
                columns: ['productId', 'unitPrice', 'leadTimeDays', 'validFrom', 'validTo'],
                conditions: [entityDetailsSimpleCondition('supplierId')],
                actions: [...entityDetailsCrudActions('supplierId')],
                layout: { type: 'page-list' },
            },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [{ id: 'supplier-profile' }, { id: 'financial-info' }, { id: 'additional-info' }],
                    properties: [
                        { name: 'supplierCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'supplierTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isPreferred', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'rating', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'paymentTermsId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'defaultCurrencyId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'purchasingManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                update: {
                    sections: [{ id: 'supplier-profile' }, { id: 'financial-info' }, { id: 'additional-info' }],
                    properties: [
                        { name: 'supplierCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'supplierTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isPreferred', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'rating', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'paymentTermsId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'defaultCurrencyId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'purchasingManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                },
                single: {
                    title: '{{ person.displayName }}',
                    sections: [
                        { id: 'basic-info', order: 1, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'supplier-profile', order: 2, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'financial-info', order: 3, layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'additional-info', order: 4, layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'supplierCode', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'supplierTypeId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'segmentId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'since', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'isPreferred', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'rating', layout: { positions: { lg: { colSpan: 3 } } } },
                        { name: 'paymentTermsId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'defaultCurrencyId', layout: { positions: { lg: { colSpan: 6 } } } },
                        { name: 'purchasingManagerId', layout: { positions: { lg: { colSpan: 6 } } } },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [
                        createAllQueryView({
                            sorts: [{ name: 'person.displayName', dir: 'asc' }],
                        }),
                    ],
                },
            },
        },
    };
    return entityDef;
}

var supplier_entity = /*#__PURE__*/Object.freeze({
    __proto__: null,
    supplierFactory: supplierFactory
});

/**
 * Generated bundle index. Do not edit.
 */

export { AXMSupplierManagementEntityProvider, AXMSupplierManagementModule, AXMSupplierManagementPermissionDefinitionProvider, AXMSupplierMenuProvider, AXPSupplierManagementMenuKeys, AXPSupplierManagementPermissionKeys, RootConfig, supplierFactory };
//# sourceMappingURL=acorex-modules-supplier-management.mjs.map
