"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _DashHeader = _interopRequireDefault(require("../../ui/compositions/DashHeader"));

var _StagingFeed = _interopRequireDefault(require("../../ui/compositions/StagingFeed"));

var _components = require("../../ui/components");

var _Header = _interopRequireDefault(require("../../ui/components/Modal/Header"));

var _Body = _interopRequireDefault(require("../../ui/components/Modal/Body"));

var _Footer = _interopRequireDefault(require("../../ui/components/Modal/Footer"));

var _styleModule = _interopRequireDefault(require("./style.module.scss"));

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function timeSince(date) {
  var seconds = Math.floor((new Date() - date) / 1000);
  var interval = seconds / 31536000;

  if (interval > 1) {
    return "".concat(Math.floor(interval), " years");
  }

  interval = seconds / 2592000;

  if (interval > 1) {
    return "".concat(Math.floor(interval), " months");
  }

  interval = seconds / 86400;

  if (interval > 1) {
    return "".concat(Math.floor(interval), " days");
  }

  interval = seconds / 3600;

  if (interval > 1) {
    return "".concat(Math.floor(interval), " hours");
  }

  interval = seconds / 60;

  if (interval > 1) {
    return "".concat(Math.floor(interval), " minutes");
  }

  return "".concat(Math.floor(seconds), " seconds");
}

var Staging = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Staging, _React$Component);

  var _super = _createSuper(Staging);

  function Staging(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Staging);
    _this = _super.call(this, props);
    _this.modal = /*#__PURE__*/_react["default"].createRef();
    _this.state = {
      offset: 0
    };
    return _this;
  }

  (0, _createClass2["default"])(Staging, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          deploy = _this$props.deploy,
          loadMore = _this$props.loadMore,
          awaiting = _this$props.awaiting,
          deployments = _this$props.deployments,
          deployed = _this$props.deployed,
          modalTarget = _this$props.modalTarget,
          previewLink = _this$props.previewLink;

      var toggle = function toggle() {
        _this2.modal.current.toggle();
      };

      var deployButtonFn = function deployButtonFn() {
        deploy().then(function () {
          _this2.setState({
            title: /*#__PURE__*/_react["default"].createElement(_components.Title, {
              level: 4
            }, /*#__PURE__*/_react["default"].createElement("i", {
              className: ['fa fa-check-circle', _styleModule["default"]['StagingModal-HeaderIcon'], _styleModule["default"]['StagingModal-HeaderIcon_green']].join(' ')
            }), "Hoot Hoot Hooray"),
            body: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, "Your changes are live and viewable by your developer community.", /*#__PURE__*/_react["default"].createElement("img", {
              alt: "Owlbert celebrating as confetti falls",
              className: _styleModule["default"]['StagingModal-Owlbert_confetti'],
              src: "http://owlbert.io/images/owlberts-png/Celebration-Confetti.psd.png"
            })),
            footer: /*#__PURE__*/_react["default"].createElement(_components.Button, {
              onClick: toggle
            }, "Close")
          });
        })["catch"](function (e) {
          _this2.setState({
            title: /*#__PURE__*/_react["default"].createElement(_components.Title, {
              level: 4
            }, /*#__PURE__*/_react["default"].createElement("i", {
              className: ['fa fa-times-circle', _styleModule["default"]['StagingModal-HeaderIcon'], _styleModule["default"]['StagingModal-HeaderIcon_red']].join(' ')
            }), "Error"),
            body: /*#__PURE__*/_react["default"].createElement("pre", {
              className: _styleModule["default"]['StagingModal-Error']
            }, e.data.errors.$all),
            footer: /*#__PURE__*/_react["default"].createElement(_components.Button, {
              onClick: toggle
            }, "Close")
          });
        });
      };

      var confirmationModal = {
        title: /*#__PURE__*/_react["default"].createElement(_components.Title, {
          level: 4
        }, "Deploying Changes"),
        body: /*#__PURE__*/_react["default"].createElement(_components.Flex, {
          layout: "col"
        }, /*#__PURE__*/_react["default"].createElement("div", null, "Deploy your staged changes and ReadMe updates to production? You won\u2019t be able to undo after deploying."), /*#__PURE__*/_react["default"].createElement("a", {
          className: _styleModule["default"]['StagingModal-Link'],
          href: previewLink,
          rel: "noopener noreferrer",
          target: "_blank"
        }, "View Staged Changes", /*#__PURE__*/_react["default"].createElement("i", {
          className: ['fa fa-arrow-circle-up', _styleModule["default"]['StagingModal-Link-ExternalIcon']].join(' ')
        }))),
        footer: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_components.Button, {
          bem: {
            shale_text: true
          },
          onClick: toggle
        }, "Cancel"), /*#__PURE__*/_react["default"].createElement(_components.Button, {
          onClick: deployButtonFn
        }, "Deploy"))
      };

      var openConfirmationModal = function openConfirmationModal() {
        _this2.setState(confirmationModal);

        toggle();
      };

      var lastDeployed = deployments.length ? /*#__PURE__*/_react["default"].createElement("div", {
        className: _styleModule["default"]['Staging-Deployed']
      }, /*#__PURE__*/_react["default"].createElement("i", {
        className: ["icon icon-owl"]
      }), "Last deployed ", timeSince(new Date(deployments[0])), " ago") : null;

      var onLoadMore = function onLoadMore() {
        _this2.setState({
          offset: _this2.state.offset + 1
        });

        loadMore();
      };

      var DashHeaderChildren = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("a", {
        className: _styleModule["default"]['DashHeader-Link'],
        href: previewLink,
        rel: "noopener noreferrer",
        target: "_blank"
      }, /*#__PURE__*/_react["default"].createElement(_components.Button, {
        text: true
      }, "Preview", /*#__PURE__*/_react["default"].createElement("i", {
        className: ['fa fa-arrow-circle-up', _styleModule["default"]['DashHeader-ExternalIcon']].join(' ')
      }))), /*#__PURE__*/_react["default"].createElement(_components.Button, {
        onClick: openConfirmationModal
      }, "Deploy"), /*#__PURE__*/_react["default"].createElement(_components.Modal, {
        ref: this.modal,
        className: _styleModule["default"].StagingModal,
        onClose: function onClose() {},
        scrollable: true,
        size: "md",
        target: modalTarget || '#modal-target-staging'
      }, /*#__PURE__*/_react["default"].createElement(_Header["default"], {
        toggleClose: toggle
      }, this.state.title), /*#__PURE__*/_react["default"].createElement(_Body["default"], null, this.state.body), /*#__PURE__*/_react["default"].createElement(_Footer["default"], null, this.state.footer)));

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_DashHeader["default"], {
        icon: "icon icon-cloud-upload",
        title: "Staging"
      }, DashHeaderChildren), /*#__PURE__*/_react["default"].createElement("div", {
        className: _styleModule["default"].Staging
      }, lastDeployed, /*#__PURE__*/_react["default"].createElement(_StagingFeed["default"], {
        displayingDeployed: false,
        timelineContent: awaiting,
        title: "Awaiting Deploy"
      }), /*#__PURE__*/_react["default"].createElement(_StagingFeed["default"], {
        displayingDeployed: true,
        loadMore: loadMore,
        timelineContent: deployed,
        title: "Deployed"
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: _styleModule["default"]['Staging-LoadMore']
      }, /*#__PURE__*/_react["default"].createElement(_components.Button, {
        disabled: deployments.length <= 1 || this.state.offset >= deployments.length - 1,
        ghost: true,
        onClick: onLoadMore
      }, "Load More"))));
    }
  }]);
  return Staging;
}(_react["default"].Component);

var Entry = _propTypes["default"].shape({
  auditlogs: _propTypes["default"].array,
  changelogs: _propTypes["default"].array,
  date: _propTypes["default"].instanceOf(Date)
});

Staging.propTypes = {
  awaiting: _propTypes["default"].arrayOf(Entry).isRequired,
  deploy: _propTypes["default"].func.isRequired,
  deployed: _propTypes["default"].arrayOf(Entry).isRequired,
  deployments: _propTypes["default"].arrayOf(Date).isRequired,
  loadMore: _propTypes["default"].func.isRequired,
  modalTarget: _propTypes["default"].string,
  previewLink: _propTypes["default"].string.isRequired
};
var _default = Staging;
exports["default"] = _default;