"use client";

import { createContext, useContext, useState, useEffect } from "react";
import citiesData from "@/data/city.json";

const ProvinceContext = createContext();

export const ProvinceProvider = ({ children }) => {
  const [hostName, setHostName] = useState("");
  const isProvinceInclude = citiesData.find((city) =>
    hostName.includes(city.domain.split(".")[0]),
  );

  useEffect(() => {
    if (typeof window !== "undefined") {
      setHostName(window.location.hostname);
    }
  }, []);

  return (
    <ProvinceContext.Provider value={{ isProvinceInclude }}>
      {children}
    </ProvinceContext.Provider>
  );
};

export const useProvince = () => useContext(ProvinceContext);
