// packages/block-editor/src/hooks/block-fields/index.js
import { addFilter } from "@wordpress/hooks";
import { privateApis as blocksPrivateApis } from "@wordpress/blocks";
import {
  __experimentalHStack as HStack,
  __experimentalTruncate as Truncate
} from "@wordpress/components";
import { createHigherOrderComponent } from "@wordpress/compose";
import { DataForm } from "@wordpress/dataviews";
import { useContext, useState, useMemo } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import { unlock } from "../../lock-unlock.mjs";
import BlockIcon from "../../components/block-icon/index.mjs";
import useBlockDisplayTitle from "../../components/block-title/use-block-display-title.mjs";
import useBlockDisplayInformation from "../../components/use-block-display-information/index.mjs";
import FieldsDropdownMenu from "./fields-dropdown-menu.mjs";
import { PrivateBlockContext } from "../../components/block-list/private-block-context.mjs";
import { PrivateInspectorControlsFill } from "../../components/inspector-controls/fill.mjs";
import RichText from "./rich-text/index.mjs";
import Media from "./media/index.mjs";
import Link from "./link/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { fieldsKey, formKey } = unlock(blocksPrivateApis);
var CONTROLS = {
  richtext: RichText,
  media: Media,
  link: Link
};
function createConfiguredControl(ControlComponent, type, config) {
  if (!ControlComponent) {
    throw new Error(`Control type "${type}" not found`);
  }
  return function ConfiguredControl(props) {
    return /* @__PURE__ */ jsx(ControlComponent, { ...props, config });
  };
}
function BlockFields({
  clientId,
  blockType,
  attributes,
  setAttributes,
  isCollapsed = false
}) {
  const blockTitle = useBlockDisplayTitle({
    clientId,
    context: "list-view"
  });
  const blockInformation = useBlockDisplayInformation(clientId);
  const blockTypeFields = blockType?.[fieldsKey];
  const computedForm = useMemo(() => {
    if (!isCollapsed) {
      return blockType?.[formKey];
    }
    return {
      ...blockType?.[formKey],
      fields: [blockType?.[formKey]?.fields?.[0]]
    };
  }, [blockType, isCollapsed]);
  const [form, setForm] = useState(computedForm);
  const dataFormFields = useMemo(() => {
    if (!blockTypeFields?.length) {
      return [];
    }
    return blockTypeFields.map((fieldDef) => {
      const field = {
        id: fieldDef.id,
        label: fieldDef.label,
        type: fieldDef.type
        // Use the field's type; DataForm will use built-in or custom Edit
      };
      if (fieldDef.mapping) {
        field.getValue = ({ item }) => {
          const mappedValue = {};
          Object.entries(fieldDef.mapping).forEach(
            ([key, attrKey]) => {
              mappedValue[key] = item[attrKey];
            }
          );
          return mappedValue;
        };
        field.setValue = ({ value }) => {
          const attributeUpdates = {};
          Object.entries(fieldDef.mapping).forEach(
            ([key, attrKey]) => {
              attributeUpdates[attrKey] = value[key];
            }
          );
          return attributeUpdates;
        };
      }
      const ControlComponent = CONTROLS[fieldDef.type];
      if (ControlComponent) {
        field.Edit = createConfiguredControl(
          ControlComponent,
          fieldDef.type,
          {
            clientId,
            fieldDef
          }
        );
      }
      return field;
    });
  }, [blockTypeFields, clientId]);
  if (!blockTypeFields?.length) {
    return null;
  }
  const handleToggleField = (fieldId) => {
    setForm((prev) => {
      if (prev.fields?.includes(fieldId)) {
        return {
          ...prev,
          fields: prev.fields.filter((id) => id !== fieldId)
        };
      }
      return {
        ...prev,
        fields: [...prev.fields || [], fieldId]
      };
    });
  };
  return /* @__PURE__ */ jsxs("div", { className: "block-editor-block-fields__container", children: [
    /* @__PURE__ */ jsx("div", { className: "block-editor-block-fields__header", children: /* @__PURE__ */ jsxs(HStack, { spacing: 1, children: [
      isCollapsed && /* @__PURE__ */ jsxs(Fragment, { children: [
        /* @__PURE__ */ jsx(
          BlockIcon,
          {
            className: "block-editor-block-fields__header-icon",
            icon: blockInformation?.icon
          }
        ),
        /* @__PURE__ */ jsx("h2", { className: "block-editor-block-fields__header-title", children: /* @__PURE__ */ jsx(Truncate, { numberOfLines: 1, children: blockTitle }) }),
        /* @__PURE__ */ jsx(
          FieldsDropdownMenu,
          {
            fields: dataFormFields,
            visibleFields: form.fields,
            onToggleField: handleToggleField
          }
        )
      ] }),
      !isCollapsed && /* @__PURE__ */ jsx("h2", { className: "block-editor-block-fields__header-title", children: __("Content") })
    ] }) }),
    /* @__PURE__ */ jsx(
      DataForm,
      {
        data: attributes,
        fields: dataFormFields,
        form,
        onChange: setAttributes
      }
    )
  ] });
}
var withBlockFields = createHigherOrderComponent(
  (BlockEdit) => (props) => {
    const {
      blockType,
      isSelectionWithinCurrentSection,
      isSectionBlock,
      blockEditingMode,
      isSelected
    } = useContext(PrivateBlockContext);
    const shouldShowBlockFields = window?.__experimentalContentOnlyInspectorFields;
    const blockTypeFields = blockType?.[fieldsKey];
    if (!shouldShowBlockFields || !blockTypeFields?.length) {
      return /* @__PURE__ */ jsx(BlockEdit, { ...props }, "edit");
    }
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(BlockEdit, { ...props }, "edit"),
      // Display the controls of all inner blocks for section/pattern editing.
      isSelectionWithinCurrentSection && (isSectionBlock || blockEditingMode === "contentOnly") && /* @__PURE__ */ jsx(
        PrivateInspectorControlsFill,
        {
          group: "content",
          forceDisplayControls: true,
          children: /* @__PURE__ */ jsx(
            BlockFields,
            {
              ...props,
              blockType,
              isCollapsed: true
            }
          )
        }
      ),
      !isSelectionWithinCurrentSection && isSelected && /* @__PURE__ */ jsx(PrivateInspectorControlsFill, { group: "content", children: /* @__PURE__ */ jsx(BlockFields, { ...props, blockType }) })
    ] });
  }
);
addFilter(
  "editor.BlockEdit",
  "core/content-only-controls/block-fields",
  withBlockFields
);
//# sourceMappingURL=index.mjs.map
