/* wp:polyfill */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PostEdit = PostEdit;
var _clsx = _interopRequireDefault(require("clsx"));
var _i18n = require("@wordpress/i18n");
var _dataviews = require("@wordpress/dataviews");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _components = require("@wordpress/components");
var _element = require("@wordpress/element");
var _editor = require("@wordpress/editor");
var _page = _interopRequireDefault(require("../page"));
var _lockUnlock = require("../../lock-unlock");
var _usePatternSettings = _interopRequireDefault(require("../page-patterns/use-pattern-settings"));
var _blockEditor = require("@wordpress/block-editor");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  usePostFields,
  PostCardPanel
} = (0, _lockUnlock.unlock)(_editor.privateApis);
const fieldsWithBulkEditSupport = ['title', 'status', 'date', 'author', 'comment_status'];
function PostEditForm({
  postType,
  postId
}) {
  const ids = (0, _element.useMemo)(() => postId.split(','), [postId]);
  const {
    record,
    hasFinishedResolution
  } = (0, _data.useSelect)(select => {
    const args = ['postType', postType, ids[0]];
    const {
      getEditedEntityRecord,
      hasFinishedResolution: hasFinished
    } = select(_coreData.store);
    return {
      record: ids.length === 1 ? getEditedEntityRecord(...args) : null,
      hasFinishedResolution: hasFinished('getEditedEntityRecord', args)
    };
  }, [postType, ids]);
  const [multiEdits, setMultiEdits] = (0, _element.useState)({});
  const {
    editEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    fields: _fields
  } = usePostFields({
    postType
  });
  const fields = (0, _element.useMemo)(() => _fields?.map(field => {
    if (field.id === 'status') {
      return {
        ...field,
        elements: field.elements.filter(element => element.value !== 'trash')
      };
    }
    return field;
  }), [_fields]);
  const form = (0, _element.useMemo)(() => ({
    type: 'panel',
    fields: [{
      id: 'featured_media',
      layout: 'regular'
    }, {
      id: 'status',
      label: (0, _i18n.__)('Status & Visibility'),
      children: ['status', 'password']
    }, 'author', 'date', 'slug', 'parent', 'comment_status', {
      label: (0, _i18n.__)('Template'),
      labelPosition: 'side',
      id: 'template',
      layout: 'regular'
    }].filter(field => ids.length === 1 || fieldsWithBulkEditSupport.includes(field))
  }), [ids]);
  const onChange = edits => {
    for (const id of ids) {
      if (edits.status && edits.status !== 'future' && record?.status === 'future' && new Date(record.date) > new Date()) {
        edits.date = null;
      }
      if (edits.status && edits.status === 'private' && record.password) {
        edits.password = '';
      }
      editEntityRecord('postType', postType, id, edits);
      if (ids.length > 1) {
        setMultiEdits(prev => ({
          ...prev,
          ...edits
        }));
      }
    }
  };
  (0, _element.useEffect)(() => {
    setMultiEdits({});
  }, [ids]);
  const {
    ExperimentalBlockEditorProvider
  } = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
  const settings = (0, _usePatternSettings.default)();

  /**
   * The template field depends on the block editor settings.
   * This is a workaround to ensure that the block editor settings are available.
   * For more information, see: https://github.com/WordPress/gutenberg/issues/67521
   */
  const fieldsWithDependency = (0, _element.useMemo)(() => {
    return fields.map(field => {
      if (field.id === 'template') {
        return {
          ...field,
          Edit: data => /*#__PURE__*/(0, _jsxRuntime.jsx)(ExperimentalBlockEditorProvider, {
            settings: settings,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(field.Edit, {
              ...data
            })
          })
        };
      }
      return field;
    });
  }, [fields, settings]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
    spacing: 4,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(PostCardPanel, {
      postType: postType,
      postId: ids
    }), hasFinishedResolution && /*#__PURE__*/(0, _jsxRuntime.jsx)(_dataviews.DataForm, {
      data: ids.length === 1 ? record : multiEdits,
      fields: fieldsWithDependency,
      form: form,
      onChange: onChange
    })]
  });
}
function PostEdit({
  postType,
  postId
}) {
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_page.default, {
    className: (0, _clsx.default)('edit-site-post-edit', {
      'is-empty': !postId
    }),
    label: (0, _i18n.__)('Post Edit'),
    children: [postId && /*#__PURE__*/(0, _jsxRuntime.jsx)(PostEditForm, {
      postType: postType,
      postId: postId
    }), !postId && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
      children: (0, _i18n.__)('Select a page to edit')
    })]
  });
}
//# sourceMappingURL=index.js.map