import type { FC } from 'react';
import React from 'react';
import { createHref } from '@theorchard/suite-utils';
import { Link } from 'react-router-dom';
import { useAppContext } from '../../app/appContext';
import { hasAccess, getRoute } from '../../utils';
import type { LinkProps } from 'react-router-dom';

interface RouteLinkProps extends Omit<LinkProps, 'to'> {
    routeId: string;
}

export const RouteLink: FC<RouteLinkProps> = ({ routeId, ...props }) => {
    const route = getRoute(routeId);
    const context = useAppContext();

    if (!route || !hasAccess(route, context)) return null;

    return <Link {...props} to={createHref(route.path)} />;
};
