const percentage = (value) => ({ tax_type: "percentage", tax_value: value });
const fixed = (value) => ({ tax_type: "fixed", tax_value: value });

const taxMatrix = [
  {
    serviceType: "sale",
    assetType: "land",
    ranges: [
      { min: 1, max: 9999, ...percentage(3) },
      { min: 10000, max: 99999, ...percentage(2) },
      { min: 100000, max: Number.MAX_SAFE_INTEGER, ...percentage(1) }
    ]
  },
  {
    serviceType: "gift",
    assetType: "house",
    ranges: [
      { min: 1, max: 9999, ...percentage(3) },
      { min: 10000, max: 99999, ...percentage(2) },
      { min: 100000, max: Number.MAX_SAFE_INTEGER, ...percentage(1) }
    ]
  },
  {
    serviceType: "agreement",
    assetType: "general",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...percentage(1) }]
  },
  {
    serviceType: "guarantee",
    assetType: "asset",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...percentage(1) }]
  },
  {
    serviceType: "guarantee",
    assetType: "other",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(1) }]
  },
  {
    serviceType: "evidence",
    assetType: "general",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(2) }]
  },
  {
    serviceType: "power_of_attorney",
    assetType: "general",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(30) }]
  },
  {
    serviceType: "will",
    assetType: "general",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(1) }]
  },
  {
    serviceType: "boat",
    assetType: "ownership",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(100) }]
  },
  {
    serviceType: "boat",
    assetType: "sale",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(25) }]
  },
  {
    serviceType: "investment",
    assetType: "general",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...percentage(8) }]
  },
  {
    serviceType: "sale",
    assetType: "shares",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...percentage(5) }]
  },
  {
    serviceType: "sale",
    assetType: "vehicle_small",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(15) }]
  },
  {
    serviceType: "sale",
    assetType: "vehicle_large",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(20) }]
  },
  {
    serviceType: "sale",
    assetType: "motorcycle",
    ranges: [{ min: 0, max: Number.MAX_SAFE_INTEGER, ...fixed(10) }]
  }
];

export const findRule = (serviceType, assetType, amount) => {
  const group =
    taxMatrix.find(
      (item) => item.serviceType === serviceType && item.assetType === assetType
    ) ||
    taxMatrix.find(
      (item) => item.serviceType === serviceType && item.assetType === "general"
    );

  if (!group) {
    return { tax_type: "fixed", tax_value: 0 };
  }

  return (
    group.ranges.find((rule) => amount >= rule.min && amount <= rule.max) || {
      tax_type: "fixed",
      tax_value: 0
    }
  );
};

export const calculateTax = (serviceType, assetType, amount) => {
  const rule = findRule(serviceType, assetType, amount);

  if (rule.tax_type === "percentage") {
    return Number((amount * (rule.tax_value / 100)).toFixed(2));
  }

  return Number(rule.tax_value.toFixed ? rule.tax_value.toFixed(2) : rule.tax_value);
};
