/* eslint-disable array-callback-return */ import dayjs from "dayjs"; import isEmpty from "lodash-es/isEmpty"; import { usePageInfo } from "@/hooks/use-page-info"; import useRoom from "@/hooks/use-room/useRoom"; import type { RoomType } from "@/hooks/use-room/useRoom.types"; import type { IHotelSearchRequest } from "@/services/eva-display/hotel/hotel-search/hotel-search-services.types"; import { useLocalizationStore } from "@/store/localization-store/use-localization-store"; export interface IHotelSaerch { destination: { id: number; type: number; title: string; typeName: string; }; date: { checkInDate: Date; checkOutDate: Date; }; rooms: RoomType[]; target?: "_blank" | "_self" | "_parent" | "_top"; originCode?: string; originId?: string; } const DESTINATIONSUB_TYPES = [ { name: "Zone", value: 0, }, { name: "Continent", value: 1, }, { name: "Country", value: 2, }, { name: "State", value: 3, }, { name: "City", value: 4, }, { name: "District", value: 5, }, { name: "Station", value: 6, }, { name: "Airport", value: 7, }, { name: "StopOver", value: 8, }, { name: "Landmark", value: 9, }, { name: "Area", value: 10, }, { name: "Neighborhood", value: 11, }, { name: "Port", value: 12, }, ]; const DESTINATION_TYPES = [ { name: "Hotel", value: "otel", }, { name: "Category", value: "Kategori", }, { name: "Destination", value: "Bölge", }, { name: "Destination", value: "Destination", }, { name: "Category", value: "Category", }, { name: "Hotel", value: "Hotel", }, ]; export const useHotelSearch = () => { const DATE_FORMAT = "YYYY-MM-DD"; const { setQueryParams, info } = usePageInfo(); const { localizationState } = useLocalizationStore(); const { roomsToString, roomsFromString, getRoom } = useRoom(); function dateToString(date: Date): string { return dayjs(date).format(DATE_FORMAT); } function dateFromString(date: string): Date { return dayjs(date, DATE_FORMAT).toDate(); } function setVariables(payload: IHotelSaerch): void { const pushUrl = payload.destination.typeName === "otel" || payload.destination.typeName === "Otel" ? "/hotel-detail" : "/hotel-list"; setQueryParams( { destination: payload.destination.title, destinationId: payload.destination.id.toString(), destinationType: payload.destination.type.toString(), destinationTypeName: payload.destination.typeName, checkinDate: dateToString(payload.date.checkInDate), checkoutDate: dateToString(payload.date.checkOutDate), rooms: roomsToString(payload.rooms), }, pushUrl, payload.target, ); } function getDestionationSubTypeValue(destinationType: number) { const destination = DESTINATIONSUB_TYPES.find( (type) => type.value === destinationType, )!; return destination.name; } function getDestionationTypeValue(destinationType: string) { const destination = DESTINATION_TYPES.find( (type) => type.value === destinationType, )!; return destination?.name; } function getVariables(): IHotelSaerch | null { if (info) { return { destination: { id: Number(info!.queryParams.destinationId), type: Number(info!.queryParams.destinationType), title: info!.queryParams.destination!, typeName: info!.queryParams.destinationTypeName!, }, date: { checkInDate: dateFromString(info!.queryParams.checkinDate!), checkOutDate: dateFromString(info!.queryParams.checkoutDate!), }, rooms: roomsFromString(info!.queryParams.rooms!), originCode: info!.queryParams.originCode, originId: info!.queryParams.originId, }; } return null; } function getHotelSearchParameters(): IHotelSearchRequest | null { const variables = getVariables(); if (!variables?.destination.id) return null; return { searchId: null, requestKey: null, checkInDate: dayjs(variables.date.checkInDate).format(DATE_FORMAT), checkOutDate: dayjs(variables.date.checkOutDate).format(DATE_FORMAT), countryCode: "TR", currency: localizationState.data.currency ?? "TRY", pageSize: 1, pageNumber: 1, orderBy: "Average", destinationId: variables.destination.id, destinationName: variables.destination.title, destinationType: "Hotel", destinationSubType: "City", filter: { hotelName: "", facilities: [], locations: [], mealTypes: [], themes: [], }, rooms: getRoom(variables.rooms), }; } return { setVariables, getVariables, isReady: info !== null && !isEmpty(info.queryParams), getDestionationSubTypeValue, getDestionationTypeValue, getHotelSearchParameters, getRoom, }; };