"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));

// packages/block-editor/src/hooks/block-fields/index.js
var import_hooks = require("@wordpress/hooks");
var import_blocks = require("@wordpress/blocks");
var import_components = require("@wordpress/components");
var import_compose = require("@wordpress/compose");
var import_dataviews = require("@wordpress/dataviews");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_block_icon = __toESM(require("../../components/block-icon/index.cjs"));
var import_use_block_display_title = __toESM(require("../../components/block-title/use-block-display-title.cjs"));
var import_use_block_display_information = __toESM(require("../../components/use-block-display-information/index.cjs"));
var import_fields_dropdown_menu = __toESM(require("./fields-dropdown-menu.cjs"));
var import_private_block_context = require("../../components/block-list/private-block-context.cjs");
var import_fill = require("../../components/inspector-controls/fill.cjs");
var import_rich_text = __toESM(require("./rich-text/index.cjs"));
var import_media = __toESM(require("./media/index.cjs"));
var import_link = __toESM(require("./link/index.cjs"));
var import_jsx_runtime = require("react/jsx-runtime");
var { fieldsKey, formKey } = (0, import_lock_unlock.unlock)(import_blocks.privateApis);
var CONTROLS = {
  richtext: import_rich_text.default,
  media: import_media.default,
  link: import_link.default
};
function createConfiguredControl(ControlComponent, type, config) {
  if (!ControlComponent) {
    throw new Error(`Control type "${type}" not found`);
  }
  return function ConfiguredControl(props) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ControlComponent, { ...props, config });
  };
}
function BlockFields({
  clientId,
  blockType,
  attributes,
  setAttributes,
  isCollapsed = false
}) {
  const blockTitle = (0, import_use_block_display_title.default)({
    clientId,
    context: "list-view"
  });
  const blockInformation = (0, import_use_block_display_information.default)(clientId);
  const blockTypeFields = blockType?.[fieldsKey];
  const computedForm = (0, import_element.useMemo)(() => {
    if (!isCollapsed) {
      return blockType?.[formKey];
    }
    return {
      ...blockType?.[formKey],
      fields: [blockType?.[formKey]?.fields?.[0]]
    };
  }, [blockType, isCollapsed]);
  const [form, setForm] = (0, import_element.useState)(computedForm);
  const dataFormFields = (0, import_element.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__ */ (0, import_jsx_runtime.jsxs)("div", { className: "block-editor-block-fields__container", children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-block-fields__header", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { spacing: 1, children: [
      isCollapsed && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_block_icon.default,
          {
            className: "block-editor-block-fields__header-icon",
            icon: blockInformation?.icon
          }
        ),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "block-editor-block-fields__header-title", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalTruncate, { numberOfLines: 1, children: blockTitle }) }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_fields_dropdown_menu.default,
          {
            fields: dataFormFields,
            visibleFields: form.fields,
            onToggleField: handleToggleField
          }
        )
      ] }),
      !isCollapsed && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "block-editor-block-fields__header-title", children: (0, import_i18n.__)("Content") })
    ] }) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_dataviews.DataForm,
      {
        data: attributes,
        fields: dataFormFields,
        form,
        onChange: setAttributes
      }
    )
  ] });
}
var withBlockFields = (0, import_compose.createHigherOrderComponent)(
  (BlockEdit) => (props) => {
    const {
      blockType,
      isSelectionWithinCurrentSection,
      isSectionBlock,
      blockEditingMode,
      isSelected
    } = (0, import_element.useContext)(import_private_block_context.PrivateBlockContext);
    const shouldShowBlockFields = window?.__experimentalContentOnlyInspectorFields;
    const blockTypeFields = blockType?.[fieldsKey];
    if (!shouldShowBlockFields || !blockTypeFields?.length) {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockEdit, { ...props }, "edit");
    }
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockEdit, { ...props }, "edit"),
      // Display the controls of all inner blocks for section/pattern editing.
      isSelectionWithinCurrentSection && (isSectionBlock || blockEditingMode === "contentOnly") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_fill.PrivateInspectorControlsFill,
        {
          group: "content",
          forceDisplayControls: true,
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            BlockFields,
            {
              ...props,
              blockType,
              isCollapsed: true
            }
          )
        }
      ),
      !isSelectionWithinCurrentSection && isSelected && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_fill.PrivateInspectorControlsFill, { group: "content", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockFields, { ...props, blockType }) })
    ] });
  }
);
(0, import_hooks.addFilter)(
  "editor.BlockEdit",
  "core/content-only-controls/block-fields",
  withBlockFields
);
//# sourceMappingURL=index.cjs.map
