/*
 * SPDX-FileCopyrightText: 2024 Kristóf Marussy
 *
 * SPDX-License-Identifier: MIT
 */

import Link, { type Props as LinkProps } from '@docusaurus/Link';
import { Certificate, Lock } from '@phosphor-icons/react';

import { CreativeCommonsIcon } from '@site/src/components/icons';

import styles from './licenses.module.css';

export type LicenseLinkProps = LinkProps & { label?: string };

export function LicenseLink({ children, label, ...props }: LicenseLinkProps) {
  return (
    <Link className={styles.link} {...props}>
      <Certificate weight="bold" aria-hidden="true" className={styles.icon} />
      <span className="sr-only">{label ?? 'License'}: </span>
      {children}
    </Link>
  );
}

export function MITLicenseLink() {
  return <LicenseLink to="https://spdx.org/licenses/MIT.html">MIT</LicenseLink>;
}

export function CCLicenseLink({ children, label, ...props }: LicenseLinkProps) {
  return (
    <Link className={styles.link} {...props}>
      <CreativeCommonsIcon className={styles.icon} hidden />
      <span className="sr-only">{label ?? 'Creative Commons license'}: </span>
      {children}
    </Link>
  );
}

export function CCBYLicenseLink() {
  return (
    <CCLicenseLink to="https://creativecommons.org/licenses/by/4.0/">
      CC-BY-4.0
    </CCLicenseLink>
  );
}

export function ClosedAccessLink({
  children,
  label,
  ...props
}: LicenseLinkProps) {
  return (
    <Link className={styles.link} {...props}>
      <Lock weight="bold" aria-hidden="true" className={styles.icon} />
      <span className="sr-only">{label ?? 'Closed access'}: </span>
      {children}
    </Link>
  );
}

export type SpanProps = React.DetailedHTMLProps<
  React.HTMLAttributes<HTMLSpanElement>,
  HTMLSpanElement
>;

export function LicenseText({ children, ...props }: SpanProps) {
  return (
    <span className={styles.text} {...props}>
      <Certificate weight="bold" aria-hidden="true" className={styles.icon} />
      {children}
    </span>
  );
}

export function CCLicenseText({ children, ...props }: SpanProps) {
  return (
    <span className={styles.text} {...props}>
      <CreativeCommonsIcon className={styles.icon} hidden />
      {children}
    </span>
  );
}

export function ClosedAccessText({ children, ...props }: SpanProps) {
  return (
    <span className={styles.text} {...props}>
      <Lock weight="bold" aria-hidden="true" className={styles.icon} />
      {children}
    </span>
  );
}
