import { AdjustmentReason } from "avatax/lib/enums/AdjustmentReason";
import { BoundaryLevel } from "avatax/lib/enums/BoundaryLevel";
import { ChargedTo } from "avatax/lib/enums/ChargedTo";
import { DocumentStatus } from "avatax/lib/enums/DocumentStatus";
import { DocumentType } from "avatax/lib/enums/DocumentType";
import { JurisTypeId } from "avatax/lib/enums/JurisTypeId";
import { JurisdictionType } from "avatax/lib/enums/JurisdictionType";
import { LiabilityType } from "avatax/lib/enums/LiabilityType";
import { RateType } from "avatax/lib/enums/RateType";
import { TransactionModel } from "avatax/lib/models/TransactionModel";
import { describe, expect, it } from "vitest";
import { AvataxOrderCreatedResponseTransformer } from "./avatax-order-created-response-transformer";

const MOCKED_TRANSACTION: TransactionModel = {
  id: 0,
  code: "8fc875ce-a929-4556-9f30-0165b1597d9f",
  companyId: 7799640,
  date: new Date(),
  paymentDate: new Date(),
  status: DocumentStatus.Temporary,
  type: DocumentType.SalesOrder,
  batchCode: "",
  currencyCode: "USD",
  exchangeRateCurrencyCode: "USD",
  customerUsageType: "",
  entityUseCode: "",
  customerVendorCode: "VXNlcjoyMDg0NTEwNDEw",
  customerCode: "VXNlcjoyMDg0NTEwNDEw",
  exemptNo: "",
  reconciled: false,
  locationCode: "",
  reportingLocationCode: "",
  purchaseOrderNo: "",
  referenceCode: "",
  salespersonCode: "",
  totalAmount: 107.31,
  totalExempt: 0,
  totalDiscount: 0,
  totalTax: 10.2,
  totalTaxable: 107.31,
  totalTaxCalculated: 10.2,
  adjustmentReason: AdjustmentReason.NotAdjusted,
  locked: false,
  version: 1,
  exchangeRateEffectiveDate: new Date(),
  exchangeRate: 1,
  modifiedDate: new Date(),
  modifiedUserId: 6479978,
  taxDate: new Date(),
  lines: [
    {
      id: 0,
      transactionId: 0,
      lineNumber: "1",
      customerUsageType: "",
      entityUseCode: "",
      discountAmount: 0,
      exemptAmount: 0,
      exemptCertId: 0,
      exemptNo: "",
      isItemTaxable: true,
      itemCode: "",
      lineAmount: 36.53,
      quantity: 2,
      ref1: "",
      ref2: "",
      reportingDate: new Date(),
      tax: 3.47,
      taxableAmount: 36.53,
      taxCalculated: 3.47,
      taxCode: "P0000000",
      taxCodeId: 8087,
      taxDate: new Date(),
      taxIncluded: true,
      details: [
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "06",
          jurisName: "CALIFORNIA",
          stateAssignedNo: "",
          jurisType: JurisTypeId.STA,
          jurisdictionType: JurisdictionType.State,
          nonTaxableAmount: 0,
          rate: 0.06,
          tax: 2.19,
          taxableAmount: 36.53,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA STATE TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 2.19,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 36.53,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 2.19,
          reportingTaxCalculated: 2.19,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "037",
          jurisName: "LOS ANGELES",
          stateAssignedNo: "",
          jurisType: JurisTypeId.CTY,
          jurisdictionType: JurisdictionType.County,
          nonTaxableAmount: 0,
          rate: 0.0025,
          tax: 0.09,
          taxableAmount: 36.53,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA COUNTY TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 0.09,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 36.53,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 0.09,
          reportingTaxCalculated: 0.09,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "EMAR0",
          jurisName: "LOS ANGELES COUNTY DISTRICT TAX SP",
          stateAssignedNo: "594",
          jurisType: JurisTypeId.STJ,
          jurisdictionType: JurisdictionType.Special,
          nonTaxableAmount: 0,
          rate: 0.0225,
          tax: 0.82,
          taxableAmount: 36.53,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA SPECIAL TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 0.82,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 36.53,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 0.82,
          reportingTaxCalculated: 0.82,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "EMTC0",
          jurisName: "LOS ANGELES CO LOCAL TAX SL",
          stateAssignedNo: "19",
          jurisType: JurisTypeId.STJ,
          jurisdictionType: JurisdictionType.Special,
          nonTaxableAmount: 0,
          rate: 0.01,
          tax: 0.37,
          taxableAmount: 36.53,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA SPECIAL TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 0.37,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 36.53,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 0.37,
          reportingTaxCalculated: 0.37,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
      ],
      nonPassthroughDetails: [],
      hsCode: "",
      costInsuranceFreight: 0,
      vatCode: "",
      vatNumberTypeId: 0,
    },
    {
      id: 0,
      transactionId: 0,
      lineNumber: "2",
      customerUsageType: "",
      entityUseCode: "",
      discountAmount: 0,
      exemptAmount: 0,
      exemptCertId: 0,
      exemptNo: "",
      isItemTaxable: true,
      itemCode: "Shipping",
      lineAmount: 70.78,
      quantity: 1,
      ref1: "",
      ref2: "",
      reportingDate: new Date(),
      tax: 6.73,
      taxableAmount: 70.78,
      taxCalculated: 6.73,
      taxCode: "P0000000",
      taxCodeId: 8087,
      taxDate: new Date(),
      taxIncluded: true,
      details: [
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "06",
          jurisName: "CALIFORNIA",
          stateAssignedNo: "",
          jurisType: JurisTypeId.STA,
          jurisdictionType: JurisdictionType.State,
          nonTaxableAmount: 0,
          rate: 0.06,
          tax: 4.25,
          taxableAmount: 70.78,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA STATE TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 4.25,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 70.78,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 4.25,
          reportingTaxCalculated: 4.25,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "037",
          jurisName: "LOS ANGELES",
          stateAssignedNo: "",
          jurisType: JurisTypeId.CTY,
          jurisdictionType: JurisdictionType.County,
          nonTaxableAmount: 0,
          rate: 0.0025,
          tax: 0.18,
          taxableAmount: 70.78,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA COUNTY TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 0.18,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 70.78,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 0.18,
          reportingTaxCalculated: 0.18,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "EMAR0",
          jurisName: "LOS ANGELES COUNTY DISTRICT TAX SP",
          stateAssignedNo: "594",
          jurisType: JurisTypeId.STJ,
          jurisdictionType: JurisdictionType.Special,
          nonTaxableAmount: 0,
          rate: 0.0225,
          tax: 1.59,
          taxableAmount: 70.78,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA SPECIAL TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 1.59,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 70.78,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 1.59,
          reportingTaxCalculated: 1.59,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
        {
          id: 0,
          transactionLineId: 0,
          transactionId: 0,
          country: "US",
          region: "CA",
          exemptAmount: 0,
          jurisCode: "EMTC0",
          jurisName: "LOS ANGELES CO LOCAL TAX SL",
          stateAssignedNo: "19",
          jurisType: JurisTypeId.STJ,
          jurisdictionType: JurisdictionType.Special,
          nonTaxableAmount: 0,
          rate: 0.01,
          tax: 0.71,
          taxableAmount: 70.78,
          taxType: "Use",
          taxSubTypeId: "U",
          taxName: "CA SPECIAL TAX",
          taxAuthorityTypeId: 45,
          taxCalculated: 0.71,
          rateType: RateType.General,
          rateTypeCode: "G",
          unitOfBasis: "PerCurrencyUnit",
          isNonPassThru: false,
          isFee: false,
          reportingTaxableUnits: 70.78,
          reportingNonTaxableUnits: 0,
          reportingExemptUnits: 0,
          reportingTax: 0.71,
          reportingTaxCalculated: 0.71,
          liabilityType: LiabilityType.Seller,
          chargedTo: ChargedTo.Buyer,
        },
      ],
      nonPassthroughDetails: [],
      hsCode: "",
      costInsuranceFreight: 0,
      vatCode: "",
      vatNumberTypeId: 0,
    },
  ],
  addresses: [
    {
      id: 0,
      transactionId: 0,
      boundaryLevel: BoundaryLevel.Zip5,
      line1: "123 Palm Grove Ln",
      line2: "",
      line3: "",
      city: "LOS ANGELES",
      region: "CA",
      postalCode: "90002",
      country: "US",
      taxRegionId: 4017056,
      latitude: "33.948712",
      longitude: "-118.245951",
    },
    {
      id: 0,
      transactionId: 0,
      boundaryLevel: BoundaryLevel.Zip5,
      line1: "8559 Lake Avenue",
      line2: "",
      line3: "",
      city: "New York",
      region: "NY",
      postalCode: "10001",
      country: "US",
      taxRegionId: 2088629,
      latitude: "40.748481",
      longitude: "-73.993125",
    },
  ],
  summary: [
    {
      country: "US",
      region: "CA",
      jurisType: JurisdictionType.State,
      jurisCode: "06",
      jurisName: "CALIFORNIA",
      taxAuthorityType: 45,
      stateAssignedNo: "",
      taxType: "Use",
      taxSubType: "U",
      taxName: "CA STATE TAX",
      rateType: RateType.General,
      taxable: 107.31,
      rate: 0.06,
      tax: 6.44,
      taxCalculated: 6.44,
      nonTaxable: 0,
      exemption: 0,
    },
    {
      country: "US",
      region: "CA",
      jurisType: JurisdictionType.County,
      jurisCode: "037",
      jurisName: "LOS ANGELES",
      taxAuthorityType: 45,
      stateAssignedNo: "",
      taxType: "Use",
      taxSubType: "U",
      taxName: "CA COUNTY TAX",
      rateType: RateType.General,
      taxable: 107.31,
      rate: 0.0025,
      tax: 0.27,
      taxCalculated: 0.27,
      nonTaxable: 0,
      exemption: 0,
    },
    {
      country: "US",
      region: "CA",
      jurisType: JurisdictionType.Special,
      jurisCode: "EMTC0",
      jurisName: "LOS ANGELES CO LOCAL TAX SL",
      taxAuthorityType: 45,
      stateAssignedNo: "19",
      taxType: "Use",
      taxSubType: "U",
      taxName: "CA SPECIAL TAX",
      rateType: RateType.General,
      taxable: 107.31,
      rate: 0.01,
      tax: 1.08,
      taxCalculated: 1.08,
      nonTaxable: 0,
      exemption: 0,
    },
    {
      country: "US",
      region: "CA",
      jurisType: JurisdictionType.Special,
      jurisCode: "EMAR0",
      jurisName: "LOS ANGELES COUNTY DISTRICT TAX SP",
      taxAuthorityType: 45,
      stateAssignedNo: "594",
      taxType: "Use",
      taxSubType: "U",
      taxName: "CA SPECIAL TAX",
      rateType: RateType.General,
      taxable: 107.31,
      rate: 0.0225,
      tax: 2.41,
      taxCalculated: 2.41,
      nonTaxable: 0,
      exemption: 0,
    },
  ],
};

describe("AvataxOrderCreatedResponseTransformer", () => {
  it("returns orded id in response", () => {
    const transformer = new AvataxOrderCreatedResponseTransformer();
    const result = transformer.transform(MOCKED_TRANSACTION);

    expect(result).toEqual({
      id: "8fc875ce-a929-4556-9f30-0165b1597d9f",
    });
  });
  it("throws an error when no transaction id is present", () => {
    const transformer = new AvataxOrderCreatedResponseTransformer();

    expect(() => transformer.transform({} as any)).toThrowError();
  });
});
