import * as React from 'react';
import clsx from 'clsx';
import { useRouter } from 'next/router';
import NextLink from 'next/link';
import MuiLink from '@mui/material/Link';
import { useUserLanguage } from "../i18n/index.js";
import { useDocsConfig } from "../DocsProvider/index.js";

/**
 * File to keep in sync with:
 *
 * - /packages/mui-docs/src/Link/Link.tsx
 * - /examples/material-ui-nextjs-pages-router/src/Link.js
 * - /examples/material-ui-nextjs-pages-router-ts/src/Link.tsx
 * - /examples/material-ui-nextjs-ts-v4-v5-migration/src/Link.tsx
 */
import { jsx as _jsx } from "react/jsx-runtime";
export const NextLinkComposed = /*#__PURE__*/React.forwardRef(function NextLinkComposed(props, ref) {
  const {
    to,
    linkAs,
    ...other
  } = props;
  return /*#__PURE__*/_jsx(NextLink, {
    href: to,
    as: linkAs,
    "data-no-markdown-link": "true",
    ref: ref,
    ...other
  });
});
// A styled version of the Next.js Link component:
// https://nextjs.org/docs/pages/api-reference/components/link
export const Link = /*#__PURE__*/React.forwardRef(function Link(props, ref) {
  const {
    activeClassName = 'active',
    as,
    className: classNameProps,
    href,
    linkAs: linkAsProp,
    noLinkStyle,
    ...other
  } = props;
  const router = useRouter();
  const pathname = typeof href === 'string' ? href : href?.pathname;
  const routerPathname = router.pathname.replace('/[docsTab]', '');
  const className = clsx(classNameProps, routerPathname === pathname && activeClassName);
  const userLanguage = useUserLanguage();
  const {
    LANGUAGES_IGNORE_PAGES
  } = useDocsConfig();
  let linkAs = linkAsProp || as || href;
  if (userLanguage !== 'en' && pathname && pathname.startsWith('/') && !LANGUAGES_IGNORE_PAGES(pathname) && !pathname.startsWith(`/${userLanguage}/`)) {
    linkAs = `/${userLanguage}${linkAs}`;
  }
  const nextjsProps = {
    to: href,
    linkAs
  };
  if (noLinkStyle) {
    return /*#__PURE__*/_jsx(NextLinkComposed, {
      className: className,
      ref: ref,
      ...nextjsProps,
      ...other
    });
  }
  return /*#__PURE__*/_jsx(MuiLink, {
    component: NextLinkComposed,
    className: className,
    ref: ref,
    ...nextjsProps,
    ...other
  });
});