"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var edit_exports = {};
__export(edit_exports, {
  default: () => BreadcrumbEdit
});
module.exports = __toCommonJS(edit_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_i18n = require("@wordpress/i18n");
var import_block_editor = require("@wordpress/block-editor");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_element = require("@wordpress/element");
var import_server_side_render = require("@wordpress/server-side-render");
var import_hooks = require("../utils/hooks");
const separatorDefaultValue = "/";
const typeDefaultValue = "auto";
const BREADCRUMB_TYPES = {
  auto: {
    help: (0, import_i18n.__)(
      "Try to automatically determine the best type of breadcrumb for the template."
    )
  },
  postWithAncestors: {
    help: (0, import_i18n.__)(
      "Shows breadcrumbs based on post hierarchy. Only works for hierarchical post types."
    ),
    placeholderItems: [(0, import_i18n.__)("Ancestor"), (0, import_i18n.__)("Parent")]
  },
  postWithTerms: {
    help: (0, import_i18n.__)(
      "Shows breadcrumbs based on taxonomy terms. Chooses the first taxonomy with assigned terms and includes ancestors if the taxonomy is hierarchical."
    ),
    placeholderItems: [(0, import_i18n.__)("Category")]
  }
};
function BreadcrumbEdit({
  attributes,
  setAttributes,
  context: { postId, postType, templateSlug }
}) {
  const { separator, showHomeLink, type } = attributes;
  const { post, isPostTypeHierarchical, hasTermsAssigned, isLoading } = (0, import_data.useSelect)(
    (select) => {
      if (!postType) {
        return {};
      }
      const _post = select(import_core_data.store).getEntityRecord(
        "postType",
        postType,
        postId
      );
      const postTypeObject = select(import_core_data.store).getPostType(postType);
      const postTypeHasTaxonomies = postTypeObject && postTypeObject.taxonomies.length;
      let taxonomies;
      if (postTypeHasTaxonomies) {
        taxonomies = select(import_core_data.store).getTaxonomies({
          type: postType,
          per_page: -1
        });
      }
      return {
        post: _post,
        isPostTypeHierarchical: postTypeObject?.hierarchical,
        hasTermsAssigned: _post && (taxonomies || []).filter(
          ({ visibility }) => visibility?.publicly_queryable
        ).some((taxonomy) => {
          return !!_post[taxonomy.rest_base]?.length;
        }),
        isLoading: !_post || !postTypeObject || postTypeHasTaxonomies && !taxonomies
      };
    },
    [postType, postId]
  );
  const [invalidationKey, setInvalidationKey] = (0, import_element.useState)(0);
  (0, import_element.useEffect)(() => {
    setInvalidationKey((c) => c + 1);
  }, [post]);
  const blockProps = (0, import_block_editor.useBlockProps)();
  const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)();
  const { content } = (0, import_server_side_render.useServerSideRender)({
    attributes,
    skipBlockSupportAttributes: true,
    block: "core/breadcrumbs",
    urlQueryArgs: { post_id: postId, invalidationKey }
  });
  if (isLoading) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}) });
  }
  let breadcrumbsType;
  const isSpecificSupportedTypeSet = [
    "postWithAncestors",
    "postWithTerms"
  ].includes(type);
  if (isSpecificSupportedTypeSet) {
    breadcrumbsType = type;
  } else {
    breadcrumbsType = isPostTypeHierarchical ? "postWithAncestors" : "postWithTerms";
  }
  let placeholder = null;
  const showPlaceholder = !postId || !postType || // When `templateSlug` is set only show placeholder if the post type is not.
  // This is needed because when we are showing the template in post editor we
  // want to show the real breadcrumbs if we have the post type.
  templateSlug && !postType || breadcrumbsType === "postWithAncestors" && !isPostTypeHierarchical || breadcrumbsType === "postWithTerms" && !hasTermsAssigned;
  if (showPlaceholder) {
    const placeholderItems = [
      showHomeLink && (0, import_i18n.__)("Home"),
      // For now if we are adding this in a template show a generic placeholder.
      ...templateSlug && !isSpecificSupportedTypeSet ? [(0, import_i18n.__)("Page")] : BREADCRUMB_TYPES[breadcrumbsType].placeholderItems
    ].filter(Boolean);
    placeholder = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      "nav",
      {
        style: {
          "--separator": `'${separator}'`
        },
        inert: "true",
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("ol", { children: [
          placeholderItems.map((text, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: `#breadcrumbs-pseudo-link-${index}`, children: text }) }, index)),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { "aria-current": "page", children: (0, import_i18n.__)("Current") }) })
        ] })
      }
    );
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_components.__experimentalToolsPanel,
      {
        label: (0, import_i18n.__)("Settings"),
        resetAll: () => {
          setAttributes({
            separator: separatorDefaultValue,
            showHomeLink: true,
            type: typeDefaultValue
          });
        },
        dropdownMenuProps,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Type"),
              isShownByDefault: true,
              hasValue: () => type !== typeDefaultValue,
              onDeselect: () => setAttributes({
                type: typeDefaultValue
              }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.SelectControl,
                {
                  __nextHasNoMarginBottom: true,
                  __next40pxDefaultSize: true,
                  label: (0, import_i18n.__)("Type"),
                  value: type,
                  onChange: (value) => setAttributes({ type: value }),
                  options: [
                    {
                      label: (0, import_i18n.__)("Auto"),
                      value: "auto"
                    },
                    {
                      label: (0, import_i18n.__)("Post with ancestors"),
                      value: "postWithAncestors"
                    },
                    {
                      label: (0, import_i18n.__)("Post with terms"),
                      value: "postWithTerms"
                    }
                  ],
                  help: BREADCRUMB_TYPES[type].help
                }
              )
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Show home link"),
              isShownByDefault: true,
              hasValue: () => !showHomeLink,
              onDeselect: () => setAttributes({
                showHomeLink: true
              }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.ToggleControl,
                {
                  __nextHasNoMarginBottom: true,
                  label: (0, import_i18n.__)("Show home link"),
                  onChange: (value) => setAttributes({ showHomeLink: value }),
                  checked: showHomeLink
                }
              )
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Separator"),
              isShownByDefault: true,
              hasValue: () => separator !== separatorDefaultValue,
              onDeselect: () => setAttributes({
                separator: separatorDefaultValue
              }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.TextControl,
                {
                  __nextHasNoMarginBottom: true,
                  __next40pxDefaultSize: true,
                  autoComplete: "off",
                  label: (0, import_i18n.__)("Separator"),
                  value: separator,
                  onChange: (value) => setAttributes({ separator: value }),
                  onBlur: () => {
                    if (!separator) {
                      setAttributes({
                        separator: separatorDefaultValue
                      });
                    }
                  }
                }
              )
            }
          )
        ]
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: showPlaceholder ? placeholder : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.RawHTML, { inert: "true", children: content }) })
  ] });
}
//# sourceMappingURL=edit.js.map
