import { useCreation, useMemoizedFn } from 'ahooks';
import pathToRegexp from 'path-to-regexp';

export default function useProtectedRoutes({ protectedRoutes = [] }) {
  const matchList = useCreation(() => {
    return protectedRoutes.map((item) => pathToRegexp(item, []));
  }, [protectedRoutes]);

  const checkMatch = useMemoizedFn((currentRoute = window.location.pathname) => {
    return matchList.some((item) => item.exec(currentRoute));
  });

  return {
    checkMatch,
  };
}
