import {
  MaterialIcon,
  type MaterialIconSetIconProps,
} from "@scandic-hotels/design-system/Icons/MaterialIcon"

import { ChildBedTypeEnum } from "@/constants/booking"

import type { JSX } from "react"

import { ChildBedMapEnum } from "@/types/components/bookingWidget/enums"
import {
  RoomPackageCodeEnum,
  type RoomPackageCodes,
} from "@/types/components/hotelReservation/selectRate/roomFilter"
import type { Child } from "@/types/components/hotelReservation/selectRate/selectRate"
import type { Packages } from "@/types/requests/packages"

interface IconForFeatureCodeProps {
  featureCode: RoomPackageCodes
}
export function IconForFeatureCode({
  featureCode,
  ...props
}: IconForFeatureCodeProps & MaterialIconSetIconProps): JSX.Element {
  switch (featureCode) {
    case RoomPackageCodeEnum.ACCESSIBILITY_ROOM:
      return <MaterialIcon icon="accessible" {...props} />
    case RoomPackageCodeEnum.ALLERGY_ROOM:
      return <MaterialIcon icon="mode_fan" {...props} />
    case RoomPackageCodeEnum.PET_ROOM:
    default:
      return <MaterialIcon icon="pets" {...props} />
  }
}

export const bedTypeMap: Record<number, ChildBedTypeEnum> = {
  [ChildBedMapEnum.IN_ADULTS_BED]: ChildBedTypeEnum.ParentsBed,
  [ChildBedMapEnum.IN_CRIB]: ChildBedTypeEnum.Crib,
  [ChildBedMapEnum.IN_EXTRA_BED]: ChildBedTypeEnum.ExtraBed,
  [ChildBedMapEnum.UNKNOWN]: ChildBedTypeEnum.Unknown,
}

export const invertedBedTypeMap: Record<ChildBedTypeEnum, string> = {
  [ChildBedTypeEnum.ParentsBed]: ChildBedMapEnum[ChildBedMapEnum.IN_ADULTS_BED],
  [ChildBedTypeEnum.Crib]: ChildBedMapEnum[ChildBedMapEnum.IN_CRIB],
  [ChildBedTypeEnum.ExtraBed]: ChildBedMapEnum[ChildBedMapEnum.IN_EXTRA_BED],
  [ChildBedTypeEnum.Unknown]: ChildBedMapEnum[ChildBedMapEnum.UNKNOWN],
}

export function generateChildrenString(children: Child[]): string {
  return `[${children
    .map((child) => {
      const age = child.age
      const bedType = bedTypeMap[parseInt(child.bed.toString())]
      return `${age}:${bedType}`
    })
    .join(",")}]`
}

export function sumPackages(packages: Packages | null) {
  if (!packages || !packages.length) {
    return {
      currency: undefined,
      price: 0,
    }
  }
  return packages.reduce(
    (total, pkg) => {
      total.price = total.price + pkg.localPrice.totalPrice
      return total
    },
    {
      currency: packages[0].localPrice.currency,
      price: 0,
    }
  )
}

export function sumPackagesRequestedPrice(packages: Packages | null) {
  if (!packages || !packages.length) {
    return {
      currency: undefined,
      price: 0,
    }
  }
  return packages.reduce(
    (total, pkg) => {
      total.price = total.price + pkg.requestedPrice.totalPrice
      return total
    },
    {
      currency: packages[0].requestedPrice.currency,
      price: 0,
    }
  )
}
