import { SLUG_NEWS, IGNORE_SLUGS, RESET_PASSWORD_SLUG, PROMOTION_SLUG, SIMPLE_PAGE_SLUG, } from '#lib/constants' import { IFrameGameURLEnum, SlugType } from '#lib/enums' import { useRoute, useRouter } from 'nuxt/app' import { useGames } from '#lib/composables' import { useApiFetch } from '#lib/composables/service/use-api-fetch' import { NEWS_API } from '#lib/configs' /** * Provides functionality for handling URL slugs and routing logic based on slug types. * * @returns {Object} An object with functions to handle slugs and open mobile iframe games, along with the current component. * @namespace */ export function useSlug() { const { request } = useApiFetch() const route = useRoute() const router = useRouter() const params = route.params const { getGameUrl, getIframeGameMapGameApi } = useGames() /** * Handles the current slug by validating it and determining its type. * * If the slug is valid, returns a corresponding slug type or string. If the slug is invalid, * redirects to a 404 page. * * @returns {Promise} A promise that resolves to the slug type or string if valid, or a redirect to 404 if invalid. */ const handleSlug = async (): Promise => { try { return await validateSlug(params.slug?.toString()?.replace('.html', '')) // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (ex) { void router.replace('/404') } } /** * Validates the provided slug and determines its type based on predefined constants and patterns. * * @param {string} slug - The slug to validate. * @returns {Promise} A promise that resolves to the type of the slug or throws an error if invalid. */ const validateSlug = async ( slug: string, ): Promise => { if (!IGNORE_SLUGS.includes(slug)) { const newSlug = params?.slug?.toString() if (newSlug.includes('.html')) { try { // eslint-disable-next-line @typescript-eslint/no-explicit-any const { data } = await request(NEWS_API.POST_DETAIL, { slug: newSlug.replace('.html', ''), }) if (data) { return SlugType.NEWS_DETAIL } } catch (ex) { console.error(' ~ validateSlug ~ ex:', ex) } } } const iframeUrl = Object.values(IFrameGameURLEnum).find((item) => params.slug.toString().includes(item?.slice(1)), ) if (iframeUrl) { return SlugType.IFRAMES } if (slug === RESET_PASSWORD_SLUG) { return RESET_PASSWORD_SLUG } if (PROMOTION_SLUG.includes(slug)) { return SlugType.PROMOTION_SLUG } if (SIMPLE_PAGE_SLUG.includes(slug)) { return SlugType.SIMPLE_PAGE_SLUG } if (slug === SLUG_NEWS.PAGE) { return SlugType.NEWS_SLUG } throw new Error('404') } /** * Opens a mobile iframe game based on the current slug by fetching the game URL and redirecting. * * @returns {Promise} A promise that resolves when the redirection to the game URL is complete. */ const openMobileIframeGame = async (): Promise => { try { const urlResponse = await getGameUrl( getIframeGameMapGameApi(`/${params.slug.toString()}`), ) if (urlResponse) { window.location.href = urlResponse.content } } catch (ex) { console.error(' ~ validateSlug ~ ex:', ex) } } return { handleSlug, openMobileIframeGame, } }