// packages/block-editor/src/components/inserter/block-patterns-tab/patterns-filter.js
import {
  SVG,
  Path,
  DropdownMenu,
  MenuGroup,
  MenuItemsChoice,
  ExternalLink
} from "@wordpress/components";
import { __, _x } from "@wordpress/i18n";
import { Icon } from "@wordpress/icons";
import { useMemo, createInterpolateElement } from "@wordpress/element";
import {
  myPatternsCategory,
  INSERTER_SYNC_TYPES,
  INSERTER_PATTERN_TYPES
} from "./utils";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var getShouldDisableSyncFilter = (sourceFilter) => sourceFilter !== "all" && sourceFilter !== "user";
var getShouldHideSourcesFilter = (category) => {
  return category.name === myPatternsCategory.name;
};
var PATTERN_SOURCE_MENU_OPTIONS = [
  {
    value: "all",
    label: _x("All", "patterns")
  },
  {
    value: INSERTER_PATTERN_TYPES.directory,
    label: __("Pattern Directory")
  },
  {
    value: INSERTER_PATTERN_TYPES.theme,
    label: __("Theme & Plugins")
  },
  {
    value: INSERTER_PATTERN_TYPES.user,
    label: __("User")
  }
];
function PatternsFilter({
  setPatternSyncFilter,
  setPatternSourceFilter,
  patternSyncFilter,
  patternSourceFilter,
  scrollContainerRef,
  category
}) {
  const currentPatternSourceFilter = category.name === myPatternsCategory.name ? INSERTER_PATTERN_TYPES.user : patternSourceFilter;
  const shouldDisableSyncFilter = getShouldDisableSyncFilter(
    currentPatternSourceFilter
  );
  const shouldHideSourcesFilter = getShouldHideSourcesFilter(category);
  const patternSyncMenuOptions = useMemo(
    () => [
      {
        value: "all",
        label: _x("All", "patterns")
      },
      {
        value: INSERTER_SYNC_TYPES.full,
        label: _x("Synced", "patterns"),
        disabled: shouldDisableSyncFilter
      },
      {
        value: INSERTER_SYNC_TYPES.unsynced,
        label: _x("Not synced", "patterns"),
        disabled: shouldDisableSyncFilter
      }
    ],
    [shouldDisableSyncFilter]
  );
  function handleSetSourceFilterChange(newSourceFilter) {
    setPatternSourceFilter(newSourceFilter);
    if (getShouldDisableSyncFilter(newSourceFilter)) {
      setPatternSyncFilter("all");
    }
  }
  return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
    DropdownMenu,
    {
      popoverProps: {
        placement: "right-end"
      },
      label: __("Filter patterns"),
      toggleProps: { size: "compact" },
      icon: /* @__PURE__ */ jsx(
        Icon,
        {
          icon: /* @__PURE__ */ jsx(
            SVG,
            {
              width: "24",
              height: "24",
              viewBox: "0 0 24 24",
              fill: "none",
              xmlns: "http://www.w3.org/2000/svg",
              children: /* @__PURE__ */ jsx(
                Path,
                {
                  d: "M10 17.5H14V16H10V17.5ZM6 6V7.5H18V6H6ZM8 12.5H16V11H8V12.5Z",
                  fill: "currentColor"
                }
              )
            }
          )
        }
      ),
      children: () => /* @__PURE__ */ jsxs(Fragment, { children: [
        !shouldHideSourcesFilter && /* @__PURE__ */ jsx(MenuGroup, { label: __("Source"), children: /* @__PURE__ */ jsx(
          MenuItemsChoice,
          {
            choices: PATTERN_SOURCE_MENU_OPTIONS,
            onSelect: (value) => {
              handleSetSourceFilterChange(value);
              scrollContainerRef.current?.scrollTo(
                0,
                0
              );
            },
            value: currentPatternSourceFilter
          }
        ) }),
        /* @__PURE__ */ jsx(MenuGroup, { label: __("Type"), children: /* @__PURE__ */ jsx(
          MenuItemsChoice,
          {
            choices: patternSyncMenuOptions,
            onSelect: (value) => {
              setPatternSyncFilter(value);
              scrollContainerRef.current?.scrollTo(
                0,
                0
              );
            },
            value: patternSyncFilter
          }
        ) }),
        /* @__PURE__ */ jsx("div", { className: "block-editor-inserter__patterns-filter-help", children: createInterpolateElement(
          __(
            "Patterns are available from the <Link>WordPress.org Pattern Directory</Link>, bundled in the active theme, or created by users on this site. Only patterns created on this site can be synced."
          ),
          {
            Link: /* @__PURE__ */ jsx(
              ExternalLink,
              {
                href: __(
                  "https://wordpress.org/patterns/"
                )
              }
            )
          }
        ) })
      ] })
    }
  ) });
}
export {
  PatternsFilter
};
//# sourceMappingURL=patterns-filter.js.map
