// packages/components/src/query-controls/index.tsx
import { __, _x } from "@wordpress/i18n";
import AuthorSelect from "./author-select.js";
import CategorySelect from "./category-select.js";
import FormTokenField from "../form-token-field/index.js";
import RangeControl from "../range-control/index.js";
import SelectControl from "../select-control/index.js";
import { VStack } from "../v-stack/index.js";
import { jsx as _jsx } from "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: __("Newest to oldest"),
  value: "date/desc"
}, {
  label: __("Oldest to newest"),
  value: "date/asc"
}, {
  /* translators: Label for ordering posts by title in ascending order. */
  label: __("A \u2192 Z"),
  value: "title/asc"
}, {
  /* translators: Label for ordering posts by title in descending order. */
  label: __("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__ */ _jsx(VStack, {
    spacing: "4",
    className: "components-query-controls",
    children: [onOrderChange && onOrderByChange && /* @__PURE__ */ _jsx(SelectControl, {
      __next40pxDefaultSize: true,
      label: __("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__ */ _jsx(CategorySelect, {
      __next40pxDefaultSize: true,
      categoriesList: props.categoriesList,
      label: __("Category"),
      noOptionLabel: _x("All", "categories"),
      selectedCategoryId: props.selectedCategoryId,
      onChange: props.onCategoryChange
    }, "query-controls-category-select"), isMultipleCategorySelection(props) && props.categorySuggestions && props.onCategoryChange && /* @__PURE__ */ _jsx(FormTokenField, {
      __next40pxDefaultSize: true,
      label: __("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__ */ _jsx(AuthorSelect, {
      __next40pxDefaultSize: true,
      authorList,
      label: __("Author"),
      noOptionLabel: _x("All", "authors"),
      selectedAuthorId,
      onChange: onAuthorChange
    }, "query-controls-author-select"), onNumberOfItemsChange && /* @__PURE__ */ _jsx(RangeControl, {
      __next40pxDefaultSize: true,
      label: __("Number of items"),
      value: numberOfItems,
      onChange: onNumberOfItemsChange,
      min: minItems,
      max: maxItems,
      required: true
    }, "query-controls-range-control")]
  });
}
var query_controls_default = QueryControls;
export {
  QueryControls,
  query_controls_default as default
};
//# sourceMappingURL=index.js.map
