"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 _compositions = require("../../ui/compositions");

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"));

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; } }

// Style
var classes = {};
/* istanbul ignore next */

if (typeof window !== 'undefined' || typeof _COMPILED_ === 'undefined') classes = {
  "slate": "#384248",
  "blue": "#118CFD",
  "green": "#12CA93",
  "red": "#E95F6A",
  "yellow": "#F7C543",
  "purple": "#8470BE",
  "graphite": "#4F5A66",
  "shale": "#939EAE",
  "chalk": "#c6cbd3",
  "ivory": "#F0F0F0",
  "lightGray": "#F8F8F8",
  "white": "#FFF",
  "DashHeader": "dzdDf-DashHeader",
  "DashHeader-Title": "_1wLYW-DashHeader-Title",
  "DashHeader-Title-Icon": "_2m0Xo-DashHeader-Title-Icon",
  "DashHeader-Link": "_382er-DashHeader-Link",
  "DashHeader-ExternalIcon": "_39bxR-DashHeader-ExternalIcon",
  "Staging": "_3llLM-Staging",
  "Staging-LoadMore": "_1lJQ3-Staging-LoadMore",
  "Staging-Deployed": "JOr57-Staging-Deployed",
  "Staging-Timeline": "sUWoL-Staging-Timeline",
  "Staging-Timeline-Heading": "dLKRC-Staging-Timeline-Heading",
  "Staging-Timeline-Day": "_3vthj-Staging-Timeline-Day",
  "Staging-Timeline-Day-Owlet": "Xuebm-Staging-Timeline-Day-Owlet",
  "Staging-Timeline-Day-Deployed": "_1x9Y7-Staging-Timeline-Day-Deployed",
  "Staging-Timeline-Day-Deployed-Icon": "_2uS34-Staging-Timeline-Day-Deployed-Icon",
  "Staging-Timeline-Content": "zF-0g-Staging-Timeline-Content",
  "StagingModal": "_3F0YM-StagingModal",
  "StagingModal-Link": "_31rZx-StagingModal-Link",
  "StagingModal-Link-ExternalIcon": "_3zak7-StagingModal-Link-ExternalIcon",
  "StagingModal-HeaderIcon": "_3U1vk-StagingModal-HeaderIcon",
  "StagingModal-HeaderIcon_green": "_3W2ek-StagingModal-HeaderIcon_green",
  "StagingModal-HeaderIcon_red": "_3Cb9y-StagingModal-HeaderIcon_red",
  "StagingModal-Body_owlbert": "_3bVCu-StagingModal-Body_owlbert",
  "StagingModal-Error": "nXctv-StagingModal-Error",
  "StagingModal-Owlbert_confetti": "_18RMm-StagingModal-Owlbert_confetti"
};

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', classes['StagingModal-HeaderIcon'], classes['StagingModal-HeaderIcon_green']].join(' ')
            }), "Hoot Hoot Hooray"),
            body: /*#__PURE__*/_react["default"].createElement("div", {
              className: classes['StagingModal-Body_owlbert']
            }, "Your changes are live and viewable by your developer community."),
            footer: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("img", {
              alt: "Owlbert celebrating as confetti falls",
              className: classes['StagingModal-Owlbert_confetti'],
              src: "http://owlbert.io/images/owlberts-png/Celebration-Confetti.psd.png"
            }), /*#__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', classes['StagingModal-HeaderIcon'], classes['StagingModal-HeaderIcon_red']].join(' ')
            }), "Error"),
            body: /*#__PURE__*/_react["default"].createElement("pre", {
              className: classes['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: classes['StagingModal-Link'],
          href: previewLink,
          rel: "noopener noreferrer",
          target: "_blank"
        }, "View Staged Changes", /*#__PURE__*/_react["default"].createElement("i", {
          className: ['fa fa-arrow-circle-up', classes['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 = awaiting.length && deployments.length ? /*#__PURE__*/_react["default"].createElement("div", {
        className: classes['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: classes['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', classes['DashHeader-ExternalIcon']].join(' ')
      }))), /*#__PURE__*/_react["default"].createElement(_components.Button, {
        onClick: openConfirmationModal
      }, "Deploy"), /*#__PURE__*/_react["default"].createElement(_components.Modal, {
        ref: this.modal,
        className: classes.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(_compositions.DashHeader, {
        icon: "icon icon-cloud-upload",
        title: "Staging"
      }, DashHeaderChildren), /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.Staging
      }, lastDeployed, /*#__PURE__*/_react["default"].createElement(_compositions.StagingFeed, {
        displayingDeployed: false,
        timelineContent: awaiting,
        title: "Awaiting Deploy"
      }), /*#__PURE__*/_react["default"].createElement(_compositions.StagingFeed, {
        displayingDeployed: true,
        loadMore: loadMore,
        timelineContent: deployed,
        title: "Deployed"
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: classes['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;