import { create } from "zustand"

import { trackOpenSidePeekEvent } from "@/utils/tracking"

import type { SidePeekEnum } from "@/types/components/hotelReservation/sidePeek"
import type { SafeUser } from "@/types/user"

interface SidePeekState {
  activeSidePeek: SidePeekEnum | null
  hotelId: string | null
  roomTypeCode: string | null
  showCTA: boolean
  user: SafeUser
  confirmationNumber: string
  openSidePeek: ({
    key,
    hotelId,
    roomTypeCode,
    showCTA,
    user,
    confirmationNumber,
  }: {
    key: SidePeekEnum | null
    hotelId: string
    roomTypeCode?: string
    showCTA?: boolean
    user?: SafeUser
    confirmationNumber?: string
  }) => void
  closeSidePeek: () => void
}

const useSidePeekStore = create<SidePeekState>((set) => ({
  activeSidePeek: null,
  hotelId: null,
  roomTypeCode: null,
  showCTA: true,
  user: null,
  confirmationNumber: "",
  openSidePeek: ({
    key,
    hotelId,
    roomTypeCode,
    showCTA,
    user,
    confirmationNumber,
  }) => {
    trackOpenSidePeekEvent(key, hotelId, window.location.pathname, roomTypeCode)
    set({
      activeSidePeek: key,
      hotelId,
      roomTypeCode,
      showCTA,
      user,
      confirmationNumber,
    })
  },
  closeSidePeek: () =>
    set({
      activeSidePeek: null,
      hotelId: null,
      roomTypeCode: null,
      confirmationNumber: "",
    }),
}))

export default useSidePeekStore
