import { useEffect } from "react";
import { usePage_titleState } from "../zustand/PageTitleState";
import { useLocation } from "react-router-dom";
import { match } from "path-to-regexp";

const useSetPage_title = (title: string, path: string) => {
  const { setPage_title } = usePage_titleState((state) => state);
  const { pathname } = useLocation();

  useEffect(() => {
    const matchPath = match(path, { decode: decodeURIComponent });
    const isMatching = matchPath(pathname);

    if (isMatching) {
      setPage_title(title);
    }
  }, [pathname, path, title, setPage_title]);
};

export default useSetPage_title;
