"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/query/edit/inspector-controls/index.js
var inspector_controls_exports = {};
__export(inspector_controls_exports, {
  default: () => QueryInspectorControls
});
module.exports = __toCommonJS(inspector_controls_exports);
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_i18n = require("@wordpress/i18n");
var import_compose = require("@wordpress/compose");
var import_element = require("@wordpress/element");
var import_order_control = __toESM(require("./order-control"));
var import_author_control = __toESM(require("./author-control"));
var import_parent_control = __toESM(require("./parent-control"));
var import_taxonomy_controls = require("./taxonomy-controls");
var import_format_controls = __toESM(require("./format-controls"));
var import_sticky_control = __toESM(require("./sticky-control"));
var import_per_page_control = __toESM(require("./per-page-control"));
var import_offset_controls = __toESM(require("./offset-controls"));
var import_pages_control = __toESM(require("./pages-control"));
var import_utils = require("../../utils");
var import_hooks = require("../../../utils/hooks");
var import_jsx_runtime = require("react/jsx-runtime");
function QueryInspectorControls(props) {
  const { attributes, setQuery, isSingular } = props;
  const { query } = attributes;
  const {
    order,
    orderBy,
    author: authorIds,
    pages,
    postType,
    perPage,
    offset,
    sticky,
    inherit,
    taxQuery,
    parents,
    format
  } = query;
  const allowedControls = (0, import_utils.useAllowedControls)(attributes);
  const showSticky = postType === "post";
  const {
    postTypesTaxonomiesMap,
    postTypesSelectOptions,
    postTypeFormatSupportMap
  } = (0, import_utils.usePostTypes)();
  const taxonomies = (0, import_utils.useTaxonomies)(postType);
  const isPostTypeHierarchical = (0, import_utils.useIsPostTypeHierarchical)(postType);
  const onPostTypeChange = (newValue) => {
    const updateQuery = { postType: newValue };
    const supportedTaxonomies = postTypesTaxonomiesMap[newValue];
    const updatedTaxQuery = Object.entries(taxQuery || {}).reduce(
      (accumulator, [taxonomySlug, terms]) => {
        if (supportedTaxonomies.includes(taxonomySlug)) {
          accumulator[taxonomySlug] = terms;
        }
        return accumulator;
      },
      {}
    );
    updateQuery.taxQuery = !!Object.keys(updatedTaxQuery).length ? updatedTaxQuery : void 0;
    if (newValue !== "post") {
      updateQuery.sticky = "";
    }
    updateQuery.parents = [];
    const hasFormatSupport = postTypeFormatSupportMap[newValue];
    if (!hasFormatSupport) {
      updateQuery.format = [];
    }
    setQuery(updateQuery);
  };
  const [querySearch, setQuerySearch] = (0, import_element.useState)(query.search);
  const debouncedQuerySearch = (0, import_element.useMemo)(() => {
    return (0, import_compose.debounce)((newQuerySearch) => {
      setQuery({ search: newQuerySearch });
    }, 250);
  }, [setQuery]);
  const orderByOptions = (0, import_utils.useOrderByOptions)(postType);
  const showInheritControl = (0, import_utils.isControlAllowed)(allowedControls, "inherit");
  const showPostTypeControl = !inherit && (0, import_utils.isControlAllowed)(allowedControls, "postType");
  const postTypeControlLabel = (0, import_i18n.__)("Post type");
  const postTypeControlHelp = (0, import_i18n.__)(
    "Select the type of content to display: posts, pages, or custom post types."
  );
  const showOrderControl = !inherit && (0, import_utils.isControlAllowed)(allowedControls, "order");
  const showStickyControl = !inherit && showSticky && (0, import_utils.isControlAllowed)(allowedControls, "sticky");
  const showSettingsPanel = showInheritControl || showPostTypeControl || showOrderControl || showStickyControl;
  const showTaxControl = !!taxonomies?.length && (0, import_utils.isControlAllowed)(allowedControls, "taxQuery");
  const showAuthorControl = (0, import_utils.isControlAllowed)(allowedControls, "author");
  const showSearchControl = (0, import_utils.isControlAllowed)(allowedControls, "search");
  const showParentControl = (0, import_utils.isControlAllowed)(allowedControls, "parents") && isPostTypeHierarchical;
  const postTypeHasFormatSupport = postTypeFormatSupportMap[postType];
  const showFormatControl = (0, import_data.useSelect)(
    (select) => {
      if (!postTypeHasFormatSupport || !(0, import_utils.isControlAllowed)(allowedControls, "format")) {
        return false;
      }
      const themeSupports = select(import_core_data.store).getThemeSupports();
      return themeSupports.formats && themeSupports.formats.length > 0 && themeSupports.formats.some((type) => type !== "standard");
    },
    [allowedControls, postTypeHasFormatSupport]
  );
  const showFiltersPanel = showTaxControl || showAuthorControl || showSearchControl || showParentControl || showFormatControl;
  const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)();
  const showPostCountControl = (0, import_utils.isControlAllowed)(
    allowedControls,
    "postCount"
  );
  const showOffSetControl = (0, import_utils.isControlAllowed)(allowedControls, "offset");
  const showPagesControl = (0, import_utils.isControlAllowed)(allowedControls, "pages");
  const showDisplayPanel = showPostCountControl || showOffSetControl || showPagesControl;
  const hasInheritanceWarning = isSingular && inherit;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    showSettingsPanel && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_components.__experimentalToolsPanel,
      {
        label: (0, import_i18n.__)("Settings"),
        resetAll: () => {
          setQuery({
            postType: "post",
            order: "desc",
            orderBy: "date",
            sticky: "",
            inherit: true
          });
        },
        dropdownMenuProps,
        children: [
          showInheritControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !inherit,
              label: (0, import_i18n.__)("Query type"),
              onDeselect: () => setQuery({ inherit: true }),
              isShownByDefault: true,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalVStack, { spacing: 4, children: [
                /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
                  import_components.__experimentalToggleGroupControl,
                  {
                    __next40pxDefaultSize: true,
                    __nextHasNoMarginBottom: true,
                    label: (0, import_i18n.__)("Query type"),
                    isBlock: true,
                    onChange: (value) => {
                      setQuery({
                        inherit: value === "default"
                      });
                    },
                    help: inherit ? (0, import_i18n.__)(
                      "Display a list of posts or custom post types based on the current template."
                    ) : (0, import_i18n.__)(
                      "Display a list of posts or custom post types based on specific criteria."
                    ),
                    value: !!inherit ? "default" : "custom",
                    children: [
                      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                        import_components.__experimentalToggleGroupControlOption,
                        {
                          value: "default",
                          label: (0, import_i18n.__)("Default")
                        }
                      ),
                      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                        import_components.__experimentalToggleGroupControlOption,
                        {
                          value: "custom",
                          label: (0, import_i18n.__)("Custom")
                        }
                      )
                    ]
                  }
                ),
                hasInheritanceWarning && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                  import_components.Notice,
                  {
                    status: "warning",
                    isDismissible: false,
                    children: (0, import_i18n.__)(
                      "Cannot inherit the current template query when placed inside the singular content (e.g., post, page, 404, blank)."
                    )
                  }
                )
              ] })
            }
          ),
          showPostTypeControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => postType !== "post",
              label: postTypeControlLabel,
              onDeselect: () => onPostTypeChange("post"),
              isShownByDefault: true,
              children: postTypesSelectOptions.length > 2 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.SelectControl,
                {
                  __nextHasNoMarginBottom: true,
                  __next40pxDefaultSize: true,
                  options: postTypesSelectOptions,
                  value: postType,
                  label: postTypeControlLabel,
                  onChange: onPostTypeChange,
                  help: postTypeControlHelp
                }
              ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.__experimentalToggleGroupControl,
                {
                  __nextHasNoMarginBottom: true,
                  __next40pxDefaultSize: true,
                  isBlock: true,
                  value: postType,
                  label: postTypeControlLabel,
                  onChange: onPostTypeChange,
                  help: postTypeControlHelp,
                  children: postTypesSelectOptions.map(
                    (option) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                      import_components.__experimentalToggleGroupControlOption,
                      {
                        value: option.value,
                        label: option.label
                      },
                      option.value
                    )
                  )
                }
              )
            }
          ),
          showOrderControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => order !== "desc" || orderBy !== "date",
              label: (0, import_i18n.__)("Order by"),
              onDeselect: () => setQuery({ order: "desc", orderBy: "date" }),
              isShownByDefault: true,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_order_control.default,
                {
                  ...{ order, orderBy, orderByOptions },
                  onChange: setQuery
                }
              )
            }
          ),
          showStickyControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !!sticky,
              label: (0, import_i18n.__)("Sticky posts"),
              onDeselect: () => setQuery({ sticky: "" }),
              isShownByDefault: true,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_sticky_control.default,
                {
                  value: sticky,
                  onChange: (value) => setQuery({ sticky: value })
                }
              )
            }
          )
        ]
      }
    ),
    !inherit && showDisplayPanel && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_components.__experimentalToolsPanel,
      {
        className: "block-library-query-toolspanel__display",
        label: (0, import_i18n.__)("Display"),
        resetAll: () => {
          setQuery({
            offset: 0,
            pages: 0
          });
        },
        dropdownMenuProps,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Items per page"),
              hasValue: () => perPage > 0,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_per_page_control.default,
                {
                  perPage,
                  offset,
                  onChange: setQuery
                }
              )
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Offset"),
              hasValue: () => offset > 0,
              onDeselect: () => setQuery({ offset: 0 }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_offset_controls.default,
                {
                  offset,
                  onChange: setQuery
                }
              )
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Max pages to show"),
              hasValue: () => pages > 0,
              onDeselect: () => setQuery({ pages: 0 }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_pages_control.default, { pages, onChange: setQuery })
            }
          )
        ]
      }
    ),
    !inherit && showFiltersPanel && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_components.__experimentalToolsPanel,
      {
        className: "block-library-query-toolspanel__filters",
        label: (0, import_i18n.__)("Filters"),
        resetAll: () => {
          setQuery({
            author: "",
            parents: [],
            search: "",
            taxQuery: null,
            format: []
          });
          setQuerySearch("");
        },
        dropdownMenuProps,
        children: [
          showTaxControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              label: (0, import_i18n.__)("Taxonomies"),
              hasValue: () => Object.values(taxQuery || {}).some(
                (terms) => !!terms.length
              ),
              onDeselect: () => setQuery({ taxQuery: null }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_taxonomy_controls.TaxonomyControls,
                {
                  onChange: setQuery,
                  query
                }
              )
            }
          ),
          showAuthorControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !!authorIds,
              label: (0, import_i18n.__)("Authors"),
              onDeselect: () => setQuery({ author: "" }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_author_control.default,
                {
                  value: authorIds,
                  onChange: setQuery
                }
              )
            }
          ),
          showSearchControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !!querySearch,
              label: (0, import_i18n.__)("Keyword"),
              onDeselect: () => {
                setQuery({ search: "" });
                setQuerySearch("");
              },
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.TextControl,
                {
                  __nextHasNoMarginBottom: true,
                  __next40pxDefaultSize: true,
                  label: (0, import_i18n.__)("Keyword"),
                  value: querySearch,
                  onChange: (newQuerySearch) => {
                    debouncedQuerySearch(newQuerySearch);
                    setQuerySearch(newQuerySearch);
                  }
                }
              )
            }
          ),
          showParentControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !!parents?.length,
              label: (0, import_i18n.__)("Parents"),
              onDeselect: () => setQuery({ parents: [] }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_parent_control.default,
                {
                  parents,
                  postType,
                  onChange: setQuery
                }
              )
            }
          ),
          showFormatControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_components.__experimentalToolsPanelItem,
            {
              hasValue: () => !!format?.length,
              label: (0, import_i18n.__)("Formats"),
              onDeselect: () => setQuery({ format: [] }),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_format_controls.default,
                {
                  onChange: setQuery,
                  query
                }
              )
            }
          )
        ]
      }
    )
  ] });
}
//# sourceMappingURL=index.js.map
