import type { LinkProps } from 'antd/lib/typography/Link';
import Link from 'antd/lib/typography/Link';
import QueryString from 'qs';
import { oldUacDomainMap } from '../utils/const';

export interface OldUacLinkProps extends LinkProps {
  path: string;
  query?: Record<string, any>;
}

const OldUacLink = (props: OldUacLinkProps) => {
  const { path, query, children, ...restProps } = props;

  const vanEnv = document.documentElement.dataset.vanEnv || 'stg';
  const domain = oldUacDomainMap[vanEnv];

  return (
    <Link
      {...restProps}
      disabled={!domain || !path}
      onClick={() => {
        if (query && Object.keys(query).length) {
          const s = QueryString.stringify(query);
          window.open(`${domain}/#${path}?${s}`);
        } else {
          window.open(`${domain}/#${path}`);
        }
      }}
    >
      {children}
    </Link>
  );
};

export default OldUacLink;
