// packages/block-library/src/navigation-link/shared/update-attributes.js
import { escapeHTML } from "@wordpress/escape-html";
import { safeDecodeURI, getPath } from "@wordpress/url";
var shouldSeverEntityLink = (originalUrl, newUrl) => {
  if (!originalUrl || !newUrl) {
    return false;
  }
  const normalizePath = (path) => {
    if (!path) {
      return "";
    }
    return path.replace(/\/+$/, "");
  };
  const createUrlObject = (url, baseUrl = null) => {
    try {
      const base = baseUrl || (typeof window !== "undefined" ? window.location.origin : "https://wordpress.org");
      return new URL(url, base);
    } catch (error) {
      return null;
    }
  };
  const originalUrlObj = createUrlObject(originalUrl);
  if (!originalUrlObj) {
    return true;
  }
  const newUrlObj = createUrlObject(newUrl, originalUrl);
  if (!newUrlObj) {
    return true;
  }
  const originalHostname = originalUrlObj.hostname;
  const newHostname = newUrlObj.hostname;
  const originalPath = normalizePath(getPath(originalUrlObj.toString()));
  const newPath = normalizePath(getPath(newUrlObj.toString()));
  if (originalHostname !== newHostname || originalPath !== newPath) {
    return true;
  }
  const originalP = originalUrlObj.searchParams.get("p");
  const newP = newUrlObj.searchParams.get("p");
  if (originalP && newP && originalP !== newP) {
    return true;
  }
  const originalPageId = originalUrlObj.searchParams.get("page_id");
  const newPageId = newUrlObj.searchParams.get("page_id");
  if (originalPageId && newPageId && originalPageId !== newPageId) {
    return true;
  }
  if (originalP && newPageId || originalPageId && newP) {
    return true;
  }
  return false;
};
var updateAttributes = (updatedValue = {}, setAttributes, blockAttributes = {}) => {
  const {
    label: originalLabel = "",
    kind: originalKind = "",
    type: originalType = ""
  } = blockAttributes;
  const {
    title: newLabel = "",
    // the title of any provided Post.
    label: newLabelFromLabel = "",
    // alternative to title
    url: newUrl,
    opensInNewTab,
    id: newID,
    kind: newKind = originalKind,
    type: newType = originalType
  } = updatedValue;
  const finalNewLabel = newLabel || newLabelFromLabel;
  const newLabelWithoutHttp = finalNewLabel.replace(/http(s?):\/\//gi, "");
  const newUrlWithoutHttp = newUrl?.replace(/http(s?):\/\//gi, "") ?? "";
  const useNewLabel = finalNewLabel && finalNewLabel !== originalLabel && // LinkControl without the title field relies
  // on the check below. Specifically, it assumes that
  // the URL is the same as a title.
  // This logic a) looks suspicious and b) should really
  // live in the LinkControl and not here. It's a great
  // candidate for future refactoring.
  newLabelWithoutHttp !== newUrlWithoutHttp;
  const label = useNewLabel ? escapeHTML(finalNewLabel) : originalLabel || escapeHTML(newUrlWithoutHttp);
  const type = newType === "post_tag" ? "tag" : newType.replace("-", "_");
  const isBuiltInType = ["post", "page", "tag", "category"].indexOf(type) > -1;
  const isCustomLink = !newKind && !isBuiltInType || newKind === "custom";
  const kind = isCustomLink ? "custom" : newKind;
  const attributes = {
    // Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
    ...newUrl !== void 0 ? { url: newUrl ? encodeURI(safeDecodeURI(newUrl)) : newUrl } : {},
    ...label && { label },
    ...void 0 !== opensInNewTab && { opensInNewTab },
    ...kind && { kind },
    ...type && type !== "URL" && { type }
  };
  if (newUrl && !newID && blockAttributes.id) {
    const shouldSever = shouldSeverEntityLink(
      blockAttributes.url,
      newUrl
    );
    if (shouldSever) {
      attributes.id = void 0;
      attributes.kind = "custom";
      attributes.type = "custom";
    }
  } else if (newID && Number.isInteger(newID)) {
    attributes.id = newID;
  } else if (blockAttributes.id) {
    attributes.kind = kind;
    attributes.type = type;
  }
  setAttributes(attributes);
  const finalId = "id" in attributes ? attributes.id : blockAttributes.id;
  const finalKind = "kind" in attributes ? attributes.kind : blockAttributes.kind;
  return {
    isEntityLink: !!finalId && finalKind !== "custom",
    attributes
    // Return the computed attributes object
  };
};
export {
  updateAttributes
};
//# sourceMappingURL=update-attributes.js.map
