import type { SelectedRate } from "@/types/stores/rates"
import type {
  CorporateChequeProduct,
  PriceProduct,
  VoucherProduct,
} from "@/types/trpc/routers/hotel/roomAvailability"

export function isSelectedPriceProduct(
  product: PriceProduct,
  selectedRate: SelectedRate | null,
  roomTypeCode: string
) {
  if (!selectedRate || roomTypeCode !== selectedRate.roomTypeCode) {
    return false
  }

  const { member, public: standard } = product
  let isSelected = false
  if (
    "member" in selectedRate.product &&
    selectedRate.product.member &&
    member
  ) {
    isSelected = selectedRate.product.member.rateCode === member.rateCode
  }

  if (
    "public" in selectedRate.product &&
    selectedRate.product.public &&
    standard
  ) {
    isSelected = selectedRate.product.public.rateCode === standard.rateCode
  }

  return isSelected
}

export function isSelectedCorporateCheque(
  product: CorporateChequeProduct,
  selectedRate: SelectedRate | null,
  roomTypeCode: string
) {
  if (!selectedRate || !("corporateCheque" in selectedRate.product)) {
    return false
  }

  const isSameRateCode =
    product.corporateCheque.rateCode ===
    selectedRate.product.corporateCheque.rateCode
  const isSameRoomTypeCode = selectedRate.roomTypeCode === roomTypeCode
  return isSameRateCode && isSameRoomTypeCode
}

export function isSelectedVoucher(
  product: VoucherProduct,
  selectedRate: SelectedRate | null,
  roomTypeCode: string
) {
  if (!selectedRate || !("voucher" in selectedRate.product)) {
    return false
  }

  const isSameRateCode =
    product.voucher.rateCode === selectedRate.product.voucher.rateCode
  const isSameRoomTypeCode = selectedRate.roomTypeCode === roomTypeCode
  return isSameRateCode && isSameRoomTypeCode
}
