"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/components/src/query-controls/index.tsx
var query_controls_exports = {};
__export(query_controls_exports, {
  QueryControls: () => QueryControls,
  default: () => query_controls_default
});
module.exports = __toCommonJS(query_controls_exports);
var import_i18n = require("@wordpress/i18n");
var import_author_select = __toESM(require("./author-select.cjs"), 1);
var import_category_select = __toESM(require("./category-select.cjs"), 1);
var import_form_token_field = __toESM(require("../form-token-field/index.cjs"), 1);
var import_range_control = __toESM(require("../range-control/index.cjs"), 1);
var import_select_control = __toESM(require("../select-control/index.cjs"), 1);
var import_v_stack = require("../v-stack/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var DEFAULT_MIN_ITEMS = 1;
var DEFAULT_MAX_ITEMS = 100;
var MAX_CATEGORIES_SUGGESTIONS = 20;
function isSingleCategorySelection(props) {
  return "categoriesList" in props;
}
function isMultipleCategorySelection(props) {
  return "categorySuggestions" in props;
}
var defaultOrderByOptions = [{
  label: (0, import_i18n.__)("Newest to oldest"),
  value: "date/desc"
}, {
  label: (0, import_i18n.__)("Oldest to newest"),
  value: "date/asc"
}, {
  /* translators: Label for ordering posts by title in ascending order. */
  label: (0, import_i18n.__)("A \u2192 Z"),
  value: "title/asc"
}, {
  /* translators: Label for ordering posts by title in descending order. */
  label: (0, import_i18n.__)("Z \u2192 A"),
  value: "title/desc"
}];
function QueryControls({
  authorList,
  selectedAuthorId,
  numberOfItems,
  order,
  orderBy,
  orderByOptions = defaultOrderByOptions,
  maxItems = DEFAULT_MAX_ITEMS,
  minItems = DEFAULT_MIN_ITEMS,
  onAuthorChange,
  onNumberOfItemsChange,
  onOrderChange,
  onOrderByChange,
  // Props for single OR multiple category selection are not destructured here,
  // but instead are destructured inline where necessary.
  ...props
}) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_v_stack.VStack, {
    spacing: "4",
    className: "components-query-controls",
    children: [onOrderChange && onOrderByChange && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_select_control.default, {
      __next40pxDefaultSize: true,
      label: (0, import_i18n.__)("Order by"),
      value: orderBy === void 0 || order === void 0 ? void 0 : `${orderBy}/${order}`,
      options: orderByOptions,
      onChange: (value) => {
        if (typeof value !== "string") {
          return;
        }
        const [newOrderBy, newOrder] = value.split("/");
        if (newOrder !== order) {
          onOrderChange(newOrder);
        }
        if (newOrderBy !== orderBy) {
          onOrderByChange(newOrderBy);
        }
      }
    }, "query-controls-order-select"), isSingleCategorySelection(props) && props.categoriesList && props.onCategoryChange && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_category_select.default, {
      __next40pxDefaultSize: true,
      categoriesList: props.categoriesList,
      label: (0, import_i18n.__)("Category"),
      noOptionLabel: (0, import_i18n._x)("All", "categories"),
      selectedCategoryId: props.selectedCategoryId,
      onChange: props.onCategoryChange
    }, "query-controls-category-select"), isMultipleCategorySelection(props) && props.categorySuggestions && props.onCategoryChange && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_form_token_field.default, {
      __next40pxDefaultSize: true,
      label: (0, import_i18n.__)("Categories"),
      value: props.selectedCategories && props.selectedCategories.map((item) => ({
        id: item.id,
        // Keeping the fallback to `item.value` for legacy reasons,
        // even if items of `selectedCategories` should not have a
        // `value` property.
        // @ts-expect-error
        value: item.name || item.value
      })),
      suggestions: Object.keys(props.categorySuggestions),
      onChange: props.onCategoryChange,
      maxSuggestions: MAX_CATEGORIES_SUGGESTIONS
    }, "query-controls-categories-select"), onAuthorChange && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_author_select.default, {
      __next40pxDefaultSize: true,
      authorList,
      label: (0, import_i18n.__)("Author"),
      noOptionLabel: (0, import_i18n._x)("All", "authors"),
      selectedAuthorId,
      onChange: onAuthorChange
    }, "query-controls-author-select"), onNumberOfItemsChange && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control.default, {
      __next40pxDefaultSize: true,
      label: (0, import_i18n.__)("Number of items"),
      value: numberOfItems,
      onChange: onNumberOfItemsChange,
      min: minItems,
      max: maxItems,
      required: true
    }, "query-controls-range-control")]
  });
}
var query_controls_default = QueryControls;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  QueryControls
});
//# sourceMappingURL=index.cjs.map
