// packages/block-library/src/query/edit/inspector-controls/parent-control.js
import { __ } from "@wordpress/i18n";
import { FormTokenField } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { useState, useEffect, useMemo } from "@wordpress/element";
import { useDebounce } from "@wordpress/compose";
import { getEntitiesInfo, mapToIHasNameAndId } from "../../utils";
import { jsx } from "react/jsx-runtime";
var EMPTY_ARRAY = [];
var BASE_QUERY = {
  order: "asc",
  _fields: "id,title",
  context: "view"
};
function ParentControl({ parents, postType, onChange }) {
  const [search, setSearch] = useState("");
  const [value, setValue] = useState(EMPTY_ARRAY);
  const [suggestions, setSuggestions] = useState(EMPTY_ARRAY);
  const debouncedSearch = useDebounce(setSearch, 250);
  const { searchResults, searchHasResolved } = useSelect(
    (select) => {
      if (!search) {
        return { searchResults: EMPTY_ARRAY, searchHasResolved: true };
      }
      const { getEntityRecords, hasFinishedResolution } = select(coreStore);
      const selectorArgs = [
        "postType",
        postType,
        {
          ...BASE_QUERY,
          search,
          orderby: "relevance",
          exclude: parents,
          per_page: 20
        }
      ];
      return {
        searchResults: getEntityRecords(...selectorArgs),
        searchHasResolved: hasFinishedResolution(
          "getEntityRecords",
          selectorArgs
        )
      };
    },
    [search, postType, parents]
  );
  const currentParents = useSelect(
    (select) => {
      if (!parents?.length) {
        return EMPTY_ARRAY;
      }
      const { getEntityRecords } = select(coreStore);
      return getEntityRecords("postType", postType, {
        ...BASE_QUERY,
        include: parents,
        per_page: parents.length
      });
    },
    [parents, postType]
  );
  useEffect(() => {
    if (!parents?.length) {
      setValue(EMPTY_ARRAY);
    }
    if (!currentParents?.length) {
      return;
    }
    const currentParentsInfo = getEntitiesInfo(
      mapToIHasNameAndId(currentParents, "title.rendered")
    );
    const sanitizedValue = parents.reduce((accumulator, id) => {
      const entity = currentParentsInfo.mapById[id];
      if (entity) {
        accumulator.push({
          id,
          value: entity.name
        });
      }
      return accumulator;
    }, []);
    setValue(sanitizedValue);
  }, [parents, currentParents]);
  const entitiesInfo = useMemo(() => {
    if (!searchResults?.length) {
      return EMPTY_ARRAY;
    }
    return getEntitiesInfo(
      mapToIHasNameAndId(searchResults, "title.rendered")
    );
  }, [searchResults]);
  useEffect(() => {
    if (!searchHasResolved) {
      return;
    }
    setSuggestions(entitiesInfo.names);
  }, [entitiesInfo.names, searchHasResolved]);
  const getIdByValue = (entitiesMappedByName, entity) => {
    const id = entity?.id || entitiesMappedByName?.[entity]?.id;
    if (id) {
      return id;
    }
  };
  const onParentChange = (newValue) => {
    const ids = Array.from(
      newValue.reduce((accumulator, entity) => {
        const id = getIdByValue(entitiesInfo.mapByName, entity);
        if (id) {
          accumulator.add(id);
        }
        return accumulator;
      }, /* @__PURE__ */ new Set())
    );
    setSuggestions(EMPTY_ARRAY);
    onChange({ parents: ids });
  };
  return /* @__PURE__ */ jsx(
    FormTokenField,
    {
      __next40pxDefaultSize: true,
      label: __("Parents"),
      value,
      onInputChange: debouncedSearch,
      suggestions,
      onChange: onParentChange,
      __experimentalShowHowTo: false,
      __nextHasNoMarginBottom: true
    }
  );
}
var parent_control_default = ParentControl;
export {
  parent_control_default as default
};
//# sourceMappingURL=parent-control.js.map
