import { Navigate, useParams } from "react-router-dom";

import {
  DEFAULT_CONFIGURATION_SLUG,
  getRuleEngineResource,
  ruleEngineResourcePath,
} from "@/pages/ruleEngine/config/resources";

/** Redirects old `/dashboard/rule-engine/:resource` URLs to category-based paths. */
const RuleEngineLegacyRedirect = () => {
  const { resource } = useParams<{ resource: string }>();
  const config = resource ? getRuleEngineResource(resource) : undefined;

  if (!config) {
    return <Navigate to={`/dashboard/configuration/${DEFAULT_CONFIGURATION_SLUG}`} replace />;
  }

  return <Navigate to={ruleEngineResourcePath(config.slug)} replace />;
};

export default RuleEngineLegacyRedirect;
