import dayjs from "dayjs"; import { usePageInfo } from "@/hooks/use-page-info"; import useRoom from "@/hooks/use-room/useRoom"; import { SortTypeEnums } from "@/models/sort.model"; import type { ITourSearchRequest } from "@/services/eva-display/tour/tour-search/tour-search.types"; export interface PassengerType { count: number; ages?: number[]; } export interface RoomType { passenger: PassengerType[]; } export interface ITourSearch { destination?: { id: number; type: number; title: string; typeName: string; }; isDomestic?: boolean; date?: { checkInDate: Date; checkOutDate: Date; }; rooms: RoomType[]; target?: "_blank" | "_self" | "_parent" | "_top"; } export const useTourSearch = () => { const DATE_FORMAT = "YYYY-MM-DD"; const { setQueryParams, info } = usePageInfo(); const { roomsToString, getRoom, roomsFromString } = 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: ITourSearch): void { const pushUrl = payload.destination?.typeName === "tur" || payload.destination?.typeName === "Tur" ? "/tour/detail" : "/tour/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 getVariables(): ITourSearch | 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!), }, isDomestic: info!.queryParams.isDomestic! === "true", rooms: roomsFromString(info!.queryParams.rooms!), }; } return null; } function getTourSearchParameters(): ITourSearchRequest | null { const variables = getVariables(); if (!variables) return null; return { tourId: 0, searchId: null, requestKey: null, pageSize: 10, isPaging: true, isDomestic: variables.isDomestic!, resetPage: true, startDate: dayjs(variables.date?.checkInDate).format(DATE_FORMAT), endDate: dayjs(variables.date?.checkOutDate).format(DATE_FORMAT), orderBy: SortTypeEnums.AVERAGE, categoryTypes: null, packageKeys: null, regions: null, destinations: null, cities: null, countries: null, currency: "TRY", keyword: [ `${variables.destination?.id}|${variables.destination?.type}|"text"`, ], rooms: getRoom(variables.rooms), searchFilter: { regionValues: null, campaignFilterValues: null, transportTypeValues: null, minNightValues: null, categoryValues: null, roadRouteValues: null, minPrice: null, maxPrice: null, }, }; } return { setVariables, getVariables, isReady: info !== null, getTourSearchParameters, getRoom, roomsToString, }; };