// packages/block-library/src/terms-query/edit/inspector-controls/index.js
import { __ } from "@wordpress/i18n";
import {
  __experimentalToolsPanel as ToolsPanel,
  __experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { InspectorControls } from "@wordpress/block-editor";
import { useToolsPanelDropdownMenuProps } from "../../../utils/hooks";
import { usePublicTaxonomies } from "../../utils";
import TaxonomyControl from "./taxonomy-control";
import OrderControl from "./order-control";
import EmptyTermsControl from "./empty-terms-control";
import NestedTermsControl from "./nested-terms-control";
import InheritControl from "./inherit-control";
import MaxTermsControl from "./max-terms-control";
import AdvancedControls from "./advanced-controls";
import IncludeControl from "./include-control";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function TermsQueryInspectorControls({
  attributes,
  setQuery,
  setAttributes,
  clientId,
  templateSlug
}) {
  const { termQuery, tagName: TagName } = attributes;
  const {
    taxonomy,
    orderBy,
    order,
    hideEmpty,
    inherit,
    showNested,
    perPage,
    include
  } = termQuery;
  const dropdownMenuProps = useToolsPanelDropdownMenuProps();
  const taxonomies = usePublicTaxonomies();
  const isTaxonomyHierarchical = taxonomies.find(
    (_taxonomy) => _taxonomy.slug === taxonomy
  )?.hierarchical;
  const inheritQuery = !!inherit;
  const displayInheritControl = ["taxonomy", "category", "tag", "archive"].includes(templateSlug) || templateSlug?.startsWith("taxonomy-") || templateSlug?.startsWith("category-") || templateSlug?.startsWith("tag-");
  const displayShowNestedControl = isTaxonomyHierarchical;
  const hasIncludeFilter = !!include?.length;
  const queryTypeControlLabel = __("Query type");
  const taxonomyControlLabel = __("Taxonomy");
  const orderByControlLabel = __("Order by");
  const emptyTermsControlLabel = __("Show empty terms");
  const nestedTermsControlLabel = __("Show nested terms");
  const maxTermsControlLabel = __("Max terms");
  const includeControlLabel = __("Selected terms");
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(
      ToolsPanel,
      {
        label: __("Settings"),
        resetAll: () => {
          setAttributes({
            termQuery: {
              taxonomy: "category",
              order: "asc",
              orderBy: "name",
              include: [],
              hideEmpty: true,
              showNested: false,
              inherit: false,
              perPage: 10
            }
          });
        },
        dropdownMenuProps,
        children: [
          displayInheritControl && /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => inherit !== false,
              label: queryTypeControlLabel,
              onDeselect: () => setQuery({ inherit: false }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                InheritControl,
                {
                  label: queryTypeControlLabel,
                  value: inherit,
                  onChange: setQuery
                }
              )
            }
          ),
          !inheritQuery && /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => taxonomy !== "category",
              label: taxonomyControlLabel,
              onDeselect: () => {
                setQuery({ taxonomy: "category" });
              },
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                TaxonomyControl,
                {
                  label: taxonomyControlLabel,
                  value: taxonomy,
                  onChange: (value) => (
                    // We also need to reset the include filter when changing taxonomy.
                    setQuery({ taxonomy: value, include: [] })
                  )
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => orderBy !== "name" || order !== "asc",
              label: orderByControlLabel,
              onDeselect: () => setQuery({ orderBy: "name", order: "asc" }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                OrderControl,
                {
                  label: orderByControlLabel,
                  ...{ orderBy, order },
                  onChange: (newOrderBy, newOrder) => {
                    setQuery({
                      orderBy: newOrderBy,
                      order: newOrder
                    });
                  },
                  disabled: hasIncludeFilter,
                  help: hasIncludeFilter ? __(
                    "When specific terms are selected, the order is based on their selection order."
                  ) : void 0
                }
              )
            }
          ),
          !inheritQuery && /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => !!include?.length,
              label: includeControlLabel,
              onDeselect: () => setQuery({
                include: [],
                orderBy: "name",
                order: "asc"
              }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                IncludeControl,
                {
                  label: includeControlLabel,
                  taxonomy,
                  value: include,
                  onChange: (value) => setQuery({ include: value })
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => hideEmpty !== true,
              label: emptyTermsControlLabel,
              onDeselect: () => setQuery({ hideEmpty: true }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                EmptyTermsControl,
                {
                  label: emptyTermsControlLabel,
                  value: hideEmpty,
                  onChange: (value) => setQuery({ hideEmpty: value })
                }
              )
            }
          ),
          displayShowNestedControl && /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => showNested !== false,
              label: nestedTermsControlLabel,
              onDeselect: () => setQuery({ showNested: false }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                NestedTermsControl,
                {
                  label: nestedTermsControlLabel,
                  value: showNested,
                  onChange: (value) => setQuery({ showNested: value }),
                  disabled: hasIncludeFilter,
                  help: hasIncludeFilter ? __(
                    "When specific terms are selected, only those are displayed."
                  ) : void 0
                }
              )
            }
          ),
          /* @__PURE__ */ jsx(
            ToolsPanelItem,
            {
              hasValue: () => perPage !== 10,
              label: maxTermsControlLabel,
              onDeselect: () => setQuery({ perPage: 10 }),
              isShownByDefault: true,
              children: /* @__PURE__ */ jsx(
                MaxTermsControl,
                {
                  label: maxTermsControlLabel,
                  value: perPage,
                  onChange: (value) => setQuery({ perPage: value })
                }
              )
            }
          )
        ]
      }
    ) }),
    /* @__PURE__ */ jsx(
      AdvancedControls,
      {
        TagName,
        setAttributes,
        clientId
      }
    )
  ] });
}
export {
  TermsQueryInspectorControls as default
};
//# sourceMappingURL=index.js.map
