'use client';
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var dedent = require('dedent');
var emery = require('emery');
var React = require('react');
var jsxRuntime = require('react/jsx-runtime');
var link = require('@react-aria/link');
var utils = require('@react-aria/utils');
var focus = require('@react-aria/focus');
var interactions = require('@react-aria/interactions');
var style = require('@keystar/ui/style');
var typography = require('@keystar/ui/typography');
var utils$1 = require('@keystar/ui/utils');
var button = require('@react-aria/button');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var dedent__default = /*#__PURE__*/_interopDefault(dedent);

const makeLinkComponent = render => ({
  __forwardRef__: /*#__PURE__*/React.forwardRef(render)
});
const DefaultLinkComponent = makeLinkComponent((props, ref) => /*#__PURE__*/jsxRuntime.jsx("a", {
  ref: ref,
  ...props
}));
const LinkComponentContext = /*#__PURE__*/React.createContext(DefaultLinkComponent);
const useLinkComponent = ref => {
  const linkComponent = React.useContext(LinkComponentContext);
  emery.assert(!ref || '__forwardRef__' in linkComponent, dedent__default["default"]`
      You're passing a ref to a Keystar UI link, but your app is providing a custom link component to 'VoussoirProvider' that doesn't appear to support refs.

      To fix this, you need to use Keystar UI's 'makeLinkComponent' helper function when creating your custom link component. This ensures that refs are forwarded correctly, and allows us to silence this error message.
    `);
  if ('__forwardRef__' in linkComponent) {
    return linkComponent.__forwardRef__;
  }
  return linkComponent;
};

function useTextLink(_ref) {
  let {
    autoFocus,
    prominence = 'default'
  } = _ref;
  const headingContext = typography.useHeadingContext();
  const textContext = typography.useTextContext();
  const {
    focusProps,
    isFocusVisible
  } = focus.useFocusRing({
    autoFocus
  });
  const {
    hoverProps,
    isHovered
  } = interactions.useHover({});
  const fontWeight = headingContext ? undefined : style.tokenSchema.typography.fontWeight.medium;
  const dataOptions = {
    prominence,
    hover: isHovered ? 'true' : undefined,
    focus: isFocusVisible ? 'visible' : undefined
  };
  return {
    ...utils.mergeProps(hoverProps, focusProps),
    ...utils$1.toDataAttributes(dataOptions),
    Wrapper: !textContext && !headingContext ? typography.Text : React.Fragment,
    className: style.classNames(style.css({
      color: style.tokenSchema.color.foreground.neutral,
      cursor: 'pointer',
      fontWeight,
      outline: 0,
      textDecoration: 'underline',
      textDecorationColor: style.tokenSchema.color.border.emphasis,
      textDecorationThickness: style.tokenSchema.size.border.regular,
      textUnderlineOffset: style.tokenSchema.size.border.medium,
      '&[data-hover="true"], &[data-focus="visible"]': {
        color: style.tokenSchema.color.foreground.neutralEmphasis,
        textDecorationColor: style.tokenSchema.color.foreground.neutral
      },
      '&[data-focus="visible"]': {
        textDecorationStyle: 'double'
      },
      '&[data-prominence="high"]': {
        color: style.tokenSchema.color.foreground.accent,
        textDecorationColor: style.tokenSchema.color.border.accent,
        '&[data-hover="true"], &[data-focus="visible"]': {
          textDecorationColor: style.tokenSchema.color.foreground.accent
        }
      }
    }))
  };
}

/** @private Forked variant where an "href" is provided. */
const TextLinkAnchor = /*#__PURE__*/React.forwardRef(function TextLink(props, forwardedRef) {
  const {
    children,
    download,
    href,
    hrefLang,
    ping,
    referrerPolicy,
    rel,
    target,
    ...otherProps
  } = props;
  const LinkComponent = useLinkComponent(forwardedRef);
  const domRef = utils.useObjectRef(forwardedRef);
  const {
    Wrapper,
    ...styleProps
  } = useTextLink(props);
  const {
    linkProps
  } = link.useLink(otherProps, domRef);
  return /*#__PURE__*/jsxRuntime.jsx(Wrapper, {
    children: /*#__PURE__*/jsxRuntime.jsx(LinkComponent, {
      ref: domRef,
      download: download,
      href: href,
      hrefLang: hrefLang,
      ping: ping,
      referrerPolicy: referrerPolicy,
      rel: rel,
      target: target,
      ...utils.mergeProps(linkProps, styleProps),
      children: children
    })
  });
});

const TextLinkButton = /*#__PURE__*/React.forwardRef(function TextLink(props, forwardedRef) {
  const {
    children,
    ...otherProps
  } = props;
  const domRef = utils.useObjectRef(forwardedRef);
  const {
    Wrapper,
    ...styleProps
  } = useTextLink(otherProps);
  const {
    buttonProps
  } = button.useButton({
    elementType: 'span',
    ...otherProps
  }, domRef);
  return /*#__PURE__*/jsxRuntime.jsx(Wrapper, {
    children: /*#__PURE__*/jsxRuntime.jsx("span", {
      ref: domRef,
      ...utils.mergeProps(buttonProps, styleProps),
      children: children
    })
  });
});

/**
 * Text links take users to another place in the application, and usually appear
 * within or directly following a sentence. Styled to resemble a hyperlink.
 */
const TextLink = /*#__PURE__*/React.forwardRef(function TextLink(props, forwardedRef) {
  if ('href' in props) {
    return /*#__PURE__*/jsxRuntime.jsx(TextLinkAnchor, {
      ...props,
      ref: forwardedRef
    });
  }
  return /*#__PURE__*/jsxRuntime.jsx(TextLinkButton, {
    ...props,
    ref: forwardedRef
  });
});

exports.DefaultLinkComponent = DefaultLinkComponent;
exports.LinkComponentContext = LinkComponentContext;
exports.TextLink = TextLink;
exports.makeLinkComponent = makeLinkComponent;
exports.useLinkComponent = useLinkComponent;
