"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = Link;
exports.useLink = useLink;
var _url = require("@wordpress/url");
var _router = require("@wordpress/router");
var _lockUnlock = require("../../lock-unlock");
var _isPreviewingTheme = require("../../utils/is-previewing-theme");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
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 = (0, _url.getQueryArgs)(window.location.href);
  const currentUrlWithoutArgs = (0, _url.removeQueryArgs)(window.location.href, ...Object.keys(currentArgs));
  let extraParams = {};
  if ((0, _isPreviewingTheme.isPreviewingTheme)()) {
    extraParams = {
      wp_theme_preview: (0, _isPreviewingTheme.currentlyPreviewingTheme)()
    };
  }
  const newUrl = (0, _url.addQueryArgs)(currentUrlWithoutArgs, {
    ...params,
    ...extraParams
  });
  return {
    href: newUrl,
    onClick
  };
}
function Link({
  params = {},
  state,
  replace: shouldReplace = false,
  children,
  ...props
}) {
  const {
    href,
    onClick
  } = useLink(params, state, shouldReplace);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
    href: href,
    onClick: onClick,
    ...props,
    children: children
  });
}
//# sourceMappingURL=link.js.map