import { inject, Injectable, InjectionToken, Provider } from '@angular/core';
import { ReceiptType } from '@isa/oms/data-access';

const receiptTypeTranslations = {
  [ReceiptType.NotSet]: 'Nicht gesetzt',
  [ReceiptType.ShippingNote]: 'Lieferschein',
  [ReceiptType.CreditNote]: 'Gutschrift',
  [ReceiptType.CollectiveShippingNote]: 'Sammellieferschein',
  [ReceiptType.CollectiveCreditNote]: 'Sammelgutschrift',
  [ReceiptType.BonusCardCollectiveShippingNote]: 'Bonuskarte Sammellieferschein',
  [ReceiptType.BonusCardCollectiveCreditNote]: 'Bonuskarte Sammelgutschrift',
  [ReceiptType.PaymentReceipt]: 'Zahlungsbeleg',
  [ReceiptType.Invoice]: 'Rechnung',
  [ReceiptType.CollectiveInvoice]: 'Sammelrechnung',
  [ReceiptType.ProformaInvoice]: 'Proforma-Rechnung',
  [ReceiptType.CashReceipt]: 'Kassenbeleg',
  [ReceiptType.ReturnReceipt]: 'Retourenbeleg',
};

export type ReceiptTypeTranslation = typeof receiptTypeTranslations;

export const RECEIPT_TYPE_TRANSLATION = new InjectionToken<ReceiptTypeTranslation>(
  'RECEIPT_TYPE_TRANSLATION',
  {
    factory() {
      return receiptTypeTranslations;
    },
  },
);

@Injectable({ providedIn: 'root' })
export class ReceiptTypeTranslationService {
  #translation = inject(RECEIPT_TYPE_TRANSLATION);

  translate(type: ReceiptType): string {
    return this.#translation[type] ?? ReceiptType[type];
  }
}

export function provideReceiptTypeTranslation(translation: ReceiptTypeTranslation): Provider[] {
  return [
    {
      provide: RECEIPT_TYPE_TRANSLATION,
      useValue: translation,
    },
    ReceiptTypeTranslationService,
  ];
}
