"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 _postFields = _interopRequireDefault(require("../post-fields"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  PostCardPanel
} = (0, _lockUnlock.unlock)(_editor.privateApis);
function PostEditForm({
  postType,
  postId
}) {
  const ids = (0, _element.useMemo)(() => postId.split(','), [postId]);
  const {
    record
  } = (0, _data.useSelect)(select => {
    return {
      record: ids.length === 1 ? select(_coreData.store).getEditedEntityRecord('postType', postType, ids[0]) : null
    };
  }, [postType, ids]);
  const [multiEdits, setMultiEdits] = (0, _element.useState)({});
  const {
    editEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const {
    fields: _fields
  } = (0, _postFields.default)();
  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 = {
    type: 'panel',
    fields: ['title', 'status', 'date', 'author', 'comment_status']
  };
  const onChange = edits => {
    for (const id of ids) {
      if (edits.status !== 'future' && record.status === 'future' && new Date(record.date) > new Date()) {
        edits.date = null;
      }
      if (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]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
    spacing: 4,
    children: [ids.length === 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(PostCardPanel, {
      postType: postType,
      postId: ids[0]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_dataviews.DataForm, {
      data: ids.length === 1 ? record : multiEdits,
      fields: fields,
      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