import { ReturnProcess } from "../../models";
import { ReturnReceiptValues } from "../../schemas";
import { getReturnProcessQuestions } from "./get-return-process-questions.helper";
import { getReturnInfo } from "./get-return-info.helper";
import { PropertyNullOrUndefinedError } from "@isa/common/data-access";
import { serializeReturnDetails } from "./return-details-mapping.helper";

export const returnReceiptValuesMapping = (
  process: ReturnProcess,
): ReturnReceiptValues | undefined => {
  const questions = getReturnProcessQuestions(process);
  if (!questions) {
    throw new PropertyNullOrUndefinedError("questions");
  }

  const returnInfo = getReturnInfo({
    questions,
    answers: process.answers,
  });

  if (!returnInfo) {
    throw new PropertyNullOrUndefinedError("returnInfo");
  }

  return {
    quantity: process.quantity,
    comment: returnInfo.comment,
    itemCondition: returnInfo.itemCondition,
    otherProduct: returnInfo.otherProduct,
    returnDetails: serializeReturnDetails(returnInfo.returnDetails),
    returnReason: returnInfo.returnReason,
    category: process.productCategory,
    receiptItem: {
      id: process.receiptItem.id,
    },
  };
};
