import { LanguageCode } from '@vendure/common/lib/generated-types';

import { RequestContext } from '../../../api/common/request-context';
import { Channel } from '../../../entity/channel/channel.entity';
import { TaxCategory } from '../../../entity/tax-category/tax-category.entity';
import { TaxRate } from '../../../entity/tax-rate/tax-rate.entity';
import { Zone } from '../../../entity/zone/zone.entity';

export const taxCategoryStandard = new TaxCategory({
    id: 'taxCategoryStandard',
    name: 'Standard Tax',
});
export const taxCategoryReduced = new TaxCategory({
    id: 'taxCategoryReduced',
    name: 'Reduced Tax',
});
export const zoneDefault = new Zone({
    id: 'zoneDefault',
    name: 'Default Zone',
});
export const zoneOther = new Zone({
    id: 'zoneOther',
    name: 'Other Zone',
});
export const zoneWithNoTaxRate = new Zone({
    id: 'zoneWithNoTaxRate',
    name: 'Zone for which no TaxRate is configured',
});
export const taxRateDefaultStandard = new TaxRate({
    id: 'taxRateDefaultStandard',
    value: 20,
    enabled: true,
    zone: zoneDefault,
    category: taxCategoryStandard,
});
export const taxRateDefaultReduced = new TaxRate({
    id: 'taxRateDefaultReduced',
    value: 10,
    enabled: true,
    zone: zoneDefault,
    category: taxCategoryReduced,
});
export const taxRateOtherStandard = new TaxRate({
    id: 'taxRateOtherStandard',
    value: 15,
    enabled: true,
    zone: zoneOther,
    category: taxCategoryStandard,
});
export const taxRateOtherReduced = new TaxRate({
    id: 'taxRateOtherReduced',
    value: 5,
    enabled: true,
    zone: zoneOther,
    category: taxCategoryReduced,
});

export class MockConnection {
    getRepository() {
        return {
            find() {
                return Promise.resolve([
                    taxRateDefaultStandard,
                    taxRateDefaultReduced,
                    taxRateOtherStandard,
                    taxRateOtherReduced,
                ]);
            },
        };
    }
}

export function createRequestContext(pricesIncludeTax: boolean): RequestContext {
    const channel = new Channel({
        defaultTaxZone: zoneDefault,
        pricesIncludeTax,
    });
    const ctx = new RequestContext({
        channel,
        authorizedAsOwnerOnly: false,
        languageCode: LanguageCode.en,
        isAuthorized: true,
        session: {} as any,
    });
    return ctx;
}
