"use strict";

exports.__esModule = true;
exports.useStyles = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _reactIntl = require("react-intl");
var _styles = require("@material-ui/core/styles");
var _constants = require("../constants");
var _useSelectorAndUnwrap = _interopRequireDefault(require("../hooks/useSelectorAndUnwrap"));
var _InformationItem = _interopRequireDefault(require("./MaterialUI/DataDisplay/PredefinedElements/InformationItem"));
var _tasks = require("../selectors/tasks");
var _propertyHelper = require("../utils/propertyHelper");
var _useLoader = _interopRequireDefault(require("../hooks/useLoader"));
var _tasks2 = require("../actions/tasks");
var _modalProps = _interopRequireDefault(require("./MaterialUI/DataDisplay/modalProps"));
var _Modal = _interopRequireDefault(require("./MaterialUI/DataDisplay/Modal"));
var _metadata = require("../selectors/metadata");
var _sharedMessages = _interopRequireDefault(require("../sharedMessages"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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 && {}.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 () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var useStyles = exports.useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    actionPanel: {
      display: "flex",
      marginLeft: "auto",
      flex: "1 1 0",
      justifyContent: "flex-end"
    },
    taskContainer: {
      display: "flex",
      width: "100%",
      flexDirection: "column",
      "&>div": {
        "&:last-child": {
          flex: 1,
          display: "flex",
          flexDirection: "column",
          "&>textarea": {
            overflowY: "scroll",
            resize: "none",
            flex: 1
          }
        }
      }
    }
  };
});
var isTaskCompleted = function isTaskCompleted(status) {
  switch (status) {
    case _constants.taskStatuses.faulted:
    case _constants.taskStatuses.ranToCompletion:
    case _constants.taskStatuses.canceled:
    case _constants.taskStatuses.ignored:
      return true;
    default:
      return false;
  }
};
var TaskDetailsModal = function TaskDetailsModal(_ref) {
  var taskId = _ref.taskId,
    open = _ref.open,
    closeModal = _ref.closeModal;
  var classes = useStyles();
  var dispatch = (0, _reactRedux.useDispatch)();
  var taskDetails = (0, _useSelectorAndUnwrap.default)((0, _tasks.taskInfo)(taskId));
  var logs = (0, _useSelectorAndUnwrap.default)((0, _tasks.taskLogs)(taskId));
  var logsText = logs.sort(function (a, b) {
    return (0, _propertyHelper.compareObjectProperty)(a, b, "executionTime");
  }).reduce(function (accumulator, currentValue) {
    if (currentValue.message === null) {
      return accumulator;
    }
    if (accumulator === "") {
      return currentValue.message.trim();
    }
    return accumulator + "\n" + currentValue.message.trim();
  }, "");
  (0, _useLoader.default)((0, _tasks2.getTaskInfo)(taskId), function () {
    return taskDetails !== null;
  });
  var internalCloseModal = function internalCloseModal() {
    dispatch((0, _tasks2.clearTaskLog)(taskId));
    closeModal();
  };
  var taskStatus = taskDetails == null ? void 0 : taskDetails.status;
  (0, _react.useEffect)(function () {
    var timer = setInterval(function () {
      if (!isTaskCompleted(taskStatus)) {
        dispatch((0, _tasks2.getTaskInfo)(taskId));
        dispatch((0, _tasks2.getTaskLog)(taskId, false));
      }
    }, 10000);
    return function () {
      return clearInterval(timer);
    };
  }, [dispatch, taskId, taskStatus]);
  var localizedStatus = (0, _reactRedux.useSelector)((0, _metadata.namedLookupLocalizedSelector)("order", "TaskStatus", taskStatus));
  var modalProps = new _modalProps.default();
  var titleComponent = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.taskInProgressModalTitle);
  modalProps.set(_modalProps.default.propNames.title, titleComponent);
  modalProps.set(_modalProps.default.propNames.open, open);
  modalProps.set(_modalProps.default.propNames.type, "wide");
  modalProps.set(_modalProps.default.propNames.backdropClickCallback, internalCloseModal);
  var actionPanel = /*#__PURE__*/_react.default.createElement("div", {
    className: classes.actionPanel
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    key: _sharedMessages.default.close.id,
    "data-qa": _sharedMessages.default.close.id,
    variant: "contained",
    color: "primary",
    disableElevation: true,
    onClick: function onClick(e) {
      return internalCloseModal(e);
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.close)));
  modalProps.set(_modalProps.default.propNames.actionPanel, actionPanel);
  var taskContent = /*#__PURE__*/_react.default.createElement("div", {
    className: classes.taskContainer
  }, /*#__PURE__*/_react.default.createElement(_InformationItem.default, {
    label: _sharedMessages.default.taskId
  }, taskId), /*#__PURE__*/_react.default.createElement(_InformationItem.default, {
    label: _sharedMessages.default.taskStatus
  }, localizedStatus), /*#__PURE__*/_react.default.createElement(_InformationItem.default, {
    label: _sharedMessages.default.taskLogs
  }, /*#__PURE__*/_react.default.createElement("textarea", {
    readOnly: true,
    value: logsText
  })));
  return /*#__PURE__*/_react.default.createElement(_Modal.default, {
    message: taskContent,
    modalProps: modalProps
  });
};
__signature__(TaskDetailsModal, "useStyles{classes}\nuseDispatch{dispatch}\nuseSelectorAndUnwrap{taskDetails}\nuseSelectorAndUnwrap{logs}\nuseLoader{}\nuseEffect{}\nuseSelector{localizedStatus}", function () {
  return [useStyles, _reactRedux.useDispatch, _useSelectorAndUnwrap.default, _useSelectorAndUnwrap.default, _useLoader.default, _reactRedux.useSelector];
});
__signature__(TaskDetailsModal, "useStyles{classes}\nuseDispatch{dispatch}\nuseSelectorAndUnwrap{taskDetails}\nuseSelectorAndUnwrap{logs}\nuseLoader{}\nuseEffect{}\nuseSelector{localizedStatus}", function () {
  return [useStyles, _reactRedux.useDispatch, _useSelectorAndUnwrap.default, _useSelectorAndUnwrap.default, _useLoader.default, _reactRedux.useSelector];
});
var _default = TaskDetailsModal;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(isTaskCompleted, "isTaskCompleted", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(TaskDetailsModal, "TaskDetailsModal", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(isTaskCompleted, "isTaskCompleted", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(TaskDetailsModal, "TaskDetailsModal", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/TaskDetailsModal.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();