"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 __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
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
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/query/edit/query-content.js
var query_content_exports = {};
__export(query_content_exports, {
  default: () => QueryContent
});
module.exports = __toCommonJS(query_content_exports);
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_element = require("@wordpress/element");
var import_block_editor = require("@wordpress/block-editor");
var import_i18n = require("@wordpress/i18n");
var import_core_data = require("@wordpress/core-data");
var import_enhanced_pagination_control = __toESM(require("./inspector-controls/enhanced-pagination-control"));
var import_lock_unlock = require("../../lock-unlock");
var import_inspector_controls = __toESM(require("./inspector-controls"));
var import_enhanced_pagination_modal = __toESM(require("./enhanced-pagination-modal"));
var import_utils = require("../utils");
var import_query_toolbar = __toESM(require("./query-toolbar"));
var import_jsx_runtime = require("react/jsx-runtime");
var { HTMLElementControl } = (0, import_lock_unlock.unlock)(import_block_editor.privateApis);
var DEFAULTS_POSTS_PER_PAGE = 3;
var TEMPLATE = [["core/post-template"]];
function QueryContent({
  attributes,
  setAttributes,
  clientId,
  context,
  name
}) {
  const {
    queryId,
    query,
    enhancedPagination,
    tagName: TagName = "div",
    query: { inherit } = {}
  } = attributes;
  const { templateSlug } = context;
  const { isSingular } = (0, import_utils.getQueryContextFromTemplate)(templateSlug);
  const { __unstableMarkNextChangeAsNotPersistent } = (0, import_data.useDispatch)(import_block_editor.store);
  const instanceId = (0, import_compose.useInstanceId)(QueryContent);
  const blockProps = (0, import_block_editor.useBlockProps)();
  const innerBlocksProps = (0, import_block_editor.useInnerBlocksProps)(blockProps, {
    template: TEMPLATE
  });
  const { postsPerPage } = (0, import_data.useSelect)((select) => {
    const { getSettings } = select(import_block_editor.store);
    const { getEntityRecord, getEntityRecordEdits, canUser } = select(import_core_data.store);
    const settingPerPage = canUser("read", {
      kind: "root",
      name: "site"
    }) ? +getEntityRecord("root", "site")?.posts_per_page : +getSettings().postsPerPage;
    const editedSettingPerPage = +getEntityRecordEdits("root", "site")?.posts_per_page;
    return {
      postsPerPage: editedSettingPerPage || settingPerPage || DEFAULTS_POSTS_PER_PAGE
    };
  }, []);
  const updateQuery = (0, import_element.useCallback)(
    (newQuery) => setAttributes((prevAttributes) => ({
      query: { ...prevAttributes.query, ...newQuery }
    })),
    [setAttributes]
  );
  (0, import_element.useEffect)(() => {
    const newQuery = {};
    if (inherit && query.perPage !== postsPerPage) {
      newQuery.perPage = postsPerPage;
    } else if (!query.perPage && postsPerPage) {
      newQuery.perPage = postsPerPage;
    }
    if (!!Object.keys(newQuery).length) {
      __unstableMarkNextChangeAsNotPersistent();
      updateQuery(newQuery);
    }
  }, [
    query.perPage,
    inherit,
    postsPerPage,
    __unstableMarkNextChangeAsNotPersistent,
    updateQuery
  ]);
  (0, import_element.useEffect)(() => {
    if (!Number.isFinite(queryId)) {
      __unstableMarkNextChangeAsNotPersistent();
      setAttributes({ queryId: instanceId });
    }
  }, [
    queryId,
    instanceId,
    __unstableMarkNextChangeAsNotPersistent,
    setAttributes
  ]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.BlockControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_query_toolbar.default,
      {
        clientId,
        attributes,
        hasInnerBlocks: true
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_enhanced_pagination_modal.default,
      {
        attributes,
        setAttributes,
        clientId
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_inspector_controls.default,
      {
        name,
        attributes,
        setQuery: updateQuery,
        setAttributes,
        clientId,
        isSingular
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_block_editor.InspectorControls, { group: "advanced", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        HTMLElementControl,
        {
          tagName: TagName,
          onChange: (value) => setAttributes({ tagName: value }),
          clientId,
          options: [
            { label: (0, import_i18n.__)("Default (<div>)"), value: "div" },
            { label: "<main>", value: "main" },
            { label: "<section>", value: "section" },
            { label: "<aside>", value: "aside" }
          ]
        }
      ),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_enhanced_pagination_control.default,
        {
          enhancedPagination,
          setAttributes,
          clientId
        }
      )
    ] }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TagName, { ...innerBlocksProps })
  ] });
}
//# sourceMappingURL=query-content.js.map
