"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _base = _interopRequireDefault(require("@emotion/styled/base"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));
var _immutable = require("immutable");
var _reactPolyglot = require("react-polyglot");
var _reactRedux = require("react-redux");
var _decapCmsUiDefault = require("decap-cms-ui-default");
var _collections = require("../../actions/collections");
var _editorialWorkflow = require("../../actions/editorialWorkflow");
var _reducers = require("../../reducers");
var _publishModes = require("../../constants/publishModes");
var _WorkflowList = _interopRequireDefault(require("./WorkflowList"));
var _react2 = require("@emotion/react");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const WorkflowContainer = /*#__PURE__*/(0, _base.default)("div", {
  target: "e1t9l5ui4",
  label: "WorkflowContainer"
})("padding:", _decapCmsUiDefault.lengths.pageMargin, " 0;height:100vh;" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7348 characters] */"));
const WorkflowTop = /*#__PURE__*/(0, _base.default)("div", {
  target: "e1t9l5ui3",
  label: "WorkflowTop"
})(_decapCmsUiDefault.components.cardTop, ";" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7348 characters] */"));
const WorkflowTopRow = /*#__PURE__*/(0, _base.default)("div", {
  target: "e1t9l5ui2",
  label: "WorkflowTopRow"
})("display:flex;justify-content:space-between;span[role='button']{", _decapCmsUiDefault.shadows.dropDeep, ";}" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7348 characters] */"));
const WorkflowTopHeading = /*#__PURE__*/(0, _base.default)("h1", {
  target: "e1t9l5ui1",
  label: "WorkflowTopHeading"
})(_decapCmsUiDefault.components.cardTopHeading, ";" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7348 characters] */"));
const WorkflowTopDescription = /*#__PURE__*/(0, _base.default)("p", {
  target: "e1t9l5ui0",
  label: "WorkflowTopDescription"
})(_decapCmsUiDefault.components.cardTopDescription, ";" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7348 characters] */"));
class Workflow extends _react.Component {
  componentDidMount() {
    const {
      loadUnpublishedEntries,
      isEditorialWorkflow,
      collections
    } = this.props;
    if (isEditorialWorkflow) {
      loadUnpublishedEntries(collections);
    }
  }
  render() {
    const {
      isEditorialWorkflow,
      isOpenAuthoring,
      isFetching,
      unpublishedEntries,
      updateUnpublishedEntryStatus,
      publishUnpublishedEntry,
      deleteUnpublishedEntry,
      collections,
      t
    } = this.props;
    if (!isEditorialWorkflow) return null;
    if (isFetching) return (0, _react2.jsx)(_decapCmsUiDefault.Loader, {
      active: true
    }, t('workflow.workflow.loading'));
    const reviewCount = unpublishedEntries.get('pending_review').size;
    const readyCount = unpublishedEntries.get('pending_publish').size;
    return (0, _react2.jsx)(WorkflowContainer, null, (0, _react2.jsx)(WorkflowTop, null, (0, _react2.jsx)(WorkflowTopRow, null, (0, _react2.jsx)(WorkflowTopHeading, null, t('workflow.workflow.workflowHeading')), (0, _react2.jsx)(_decapCmsUiDefault.Dropdown, {
      dropdownWidth: "160px",
      dropdownPosition: "left",
      dropdownTopOverlap: "40px",
      renderButton: () => (0, _react2.jsx)(_decapCmsUiDefault.StyledDropdownButton, null, t('workflow.workflow.newPost'))
    }, collections.filter(collection => collection.get('create')).toList().map(collection => (0, _react2.jsx)(_decapCmsUiDefault.DropdownItem, {
      key: collection.get('name'),
      label: collection.get('label'),
      onClick: () => (0, _collections.createNewEntry)(collection.get('name'))
    })))), (0, _react2.jsx)(WorkflowTopDescription, null, t('workflow.workflow.description', {
      smart_count: reviewCount,
      readyCount
    }))), (0, _react2.jsx)(_WorkflowList.default, {
      entries: unpublishedEntries,
      handleChangeStatus: updateUnpublishedEntryStatus,
      handlePublish: publishUnpublishedEntry,
      handleDelete: deleteUnpublishedEntry,
      isOpenAuthoring: isOpenAuthoring,
      collections: collections
    }));
  }
}
_defineProperty(Workflow, "propTypes", {
  collections: _reactImmutableProptypes.default.map.isRequired,
  isEditorialWorkflow: _propTypes.default.bool.isRequired,
  isOpenAuthoring: _propTypes.default.bool,
  isFetching: _propTypes.default.bool,
  unpublishedEntries: _reactImmutableProptypes.default.map,
  loadUnpublishedEntries: _propTypes.default.func.isRequired,
  updateUnpublishedEntryStatus: _propTypes.default.func.isRequired,
  publishUnpublishedEntry: _propTypes.default.func.isRequired,
  deleteUnpublishedEntry: _propTypes.default.func.isRequired,
  t: _propTypes.default.func.isRequired
});
function mapStateToProps(state) {
  const {
    collections,
    config,
    globalUI
  } = state;
  const isEditorialWorkflow = config.publish_mode === _publishModes.EDITORIAL_WORKFLOW;
  const isOpenAuthoring = globalUI.useOpenAuthoring;
  const returnObj = {
    collections,
    isEditorialWorkflow,
    isOpenAuthoring
  };
  if (isEditorialWorkflow) {
    returnObj.isFetching = state.editorialWorkflow.getIn(['pages', 'isFetching'], false);

    /*
     * Generates an ordered Map of the available status as keys.
     * Each key containing a Sequence of available unpubhlished entries
     * Eg.: OrderedMap{'draft':Seq(), 'pending_review':Seq(), 'pending_publish':Seq()}
     */
    returnObj.unpublishedEntries = _publishModes.status.reduce((acc, currStatus) => {
      const entries = (0, _reducers.selectUnpublishedEntriesByStatus)(state, currStatus);
      return acc.set(currStatus, entries);
    }, (0, _immutable.OrderedMap)());
  }
  return returnObj;
}
var _default = exports.default = (0, _reactRedux.connect)(mapStateToProps, {
  loadUnpublishedEntries: _editorialWorkflow.loadUnpublishedEntries,
  updateUnpublishedEntryStatus: _editorialWorkflow.updateUnpublishedEntryStatus,
  publishUnpublishedEntry: _editorialWorkflow.publishUnpublishedEntry,
  deleteUnpublishedEntry: _editorialWorkflow.deleteUnpublishedEntry
})((0, _reactPolyglot.translate)()(Workflow));