/**
 * WordPress dependencies
 */
import { addQueryArgs, getQueryArgs, removeQueryArgs } from '@wordpress/url';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
 * Internal dependencies
 */
import { unlock } from '../../lock-unlock';
import { isPreviewingTheme, currentlyPreviewingTheme } from '../../utils/is-previewing-theme';
import { jsx as _jsx } from "react/jsx-runtime";
const {
  useHistory
} = unlock(routerPrivateApis);
export function useLink(params, state, shouldReplace = false) {
  const history = useHistory();
  function onClick(event) {
    event?.preventDefault();
    if (shouldReplace) {
      history.replace(params, state);
    } else {
      history.push(params, state);
    }
  }
  const currentArgs = getQueryArgs(window.location.href);
  const currentUrlWithoutArgs = removeQueryArgs(window.location.href, ...Object.keys(currentArgs));
  let extraParams = {};
  if (isPreviewingTheme()) {
    extraParams = {
      wp_theme_preview: currentlyPreviewingTheme()
    };
  }
  const newUrl = addQueryArgs(currentUrlWithoutArgs, {
    ...params,
    ...extraParams
  });
  return {
    href: newUrl,
    onClick
  };
}
export default function Link({
  params = {},
  state,
  replace: shouldReplace = false,
  children,
  ...props
}) {
  const {
    href,
    onClick
  } = useLink(params, state, shouldReplace);
  return /*#__PURE__*/_jsx("a", {
    href: href,
    onClick: onClick,
    ...props,
    children: children
  });
}
//# sourceMappingURL=link.js.map