"use strict";

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

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

var _reactHookForm = require("react-hook-form");

var _uuid = require("uuid");

var _Dropdown = require("../Dropdown");

var _data = require("./data");

var _evidenceSection = require("./evidence-section");

var _context2 = require("../../context");

var _hypothesisStatuses = require("../../constants/hypothesis-statuses");

var _jsxRuntime = require("react/jsx-runtime");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var statusItems = _objectSpread({}, Object.values(_data.hypothesisStatuses).reduce(function (acc, obj) {
  var title = obj.title.toLowerCase();
  acc[title] = {
    id: "dropdown-".concat(title),
    name: title,
    icon: /*#__PURE__*/_react.default.createElement(obj.icon.type, {
      color: obj.color,
      style: {
        width: "20px"
      }
    }),
    description: obj.title,
    value: title
  };
  return acc;
}, {}));

var nextNodePath = {
  root: "factor",
  factor: "solution"
};

var CreateHypothesis = function CreateHypothesis(_ref) {
  var node = _ref.node,
      api = _ref.api,
      onHypothesisCreated = _ref.onHypothesisCreated;
  var user = (0, _context2.useUser)();

  var _useForm = (0, _reactHookForm.useForm)({
    defaultValues: {
      hypothesis: {
        status: _hypothesisStatuses.HypothesisStatuses.Possible,
        title: ""
      },
      evidence: {},
      comment: {
        text: ""
      }
    }
  }),
      control = _useForm.control,
      getValues = _useForm.getValues,
      setValue = _useForm.setValue,
      handleSubmit = _useForm.handleSubmit,
      watch = _useForm.watch;

  var evidenceValue = watch("evidence"); // const handleComment = (nodeId, value) =>
  //   createComment(user, uuidv4(), node.incident_id, nodeId, value)
  //     .then((response) => console.log(response))
  //     .catch(toastError);

  var onSubmit = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var newNodeID, newNodeResponse;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              newNodeID = (0, _uuid.v4)();

              if (!(api !== null && api !== void 0 && api.createMutation)) {
                _context.next = 5;
                break;
              }

              _context.next = 4;
              return api.createMutation.mutateAsync({
                user: user,
                id: newNodeID,
                incidentId: api.incidentId,
                params: {
                  parent_id: node.id,
                  title: getValues("hypothesis.title"),
                  type: nextNodePath[node.type],
                  status: getValues("hypothesis.status")
                }
              });

            case 4:
              newNodeResponse = _context.sent;

            case 5:
              onHypothesisCreated();

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onSubmit() {
      return _ref2.apply(this, arguments);
    };
  }();

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("form", {
    onSubmit: handleSubmit(onSubmit),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("h2", {
      className: "text-2xl font-semibold text-gray-700",
      children: ["Create ", nextNodePath[node.type], ": ", node.title]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "mt-6",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "text-sm font-medium text-gray-700 mb-1.5",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
          htmlFor: "name",
          children: "Name"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
        name: "hypothesis.title",
        control: control,
        render: function render(_ref3) {
          var field = _ref3.field;
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("input", _objectSpread({
            className: "w-72 cursor-pointer bg-white border border-gray-300 rounded-md shadow-sm pl-3 pr-10 py-2 text-left focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm text-gray-500 text-base leading-6 font-normal",
            type: "text",
            placeholder: "Type name"
          }, field));
        }
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "mt-4",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.Dropdown, {
        control: control,
        name: "hypothesis.status",
        className: "mb-4 w-72",
        items: statusItems,
        label: "State"
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_evidenceSection.EvidenceSection, {
      hypothesis: node,
      evidence: evidenceValue,
      titlePrepend: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "mr-2 text-sm font-medium text-gray-700 mt-1.5",
        children: "Evidence"
      }),
      onButtonClick: function onButtonClick(data) {
        setValue("evidence", data);
      }
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("h3", {
        className: "mt-4 text-sm font-medium text-gray-700 mb-1.5",
        children: "Comment"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
        name: "comment.text",
        control: control,
        render: function render(_ref4) {
          var field = _ref4.field;
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", _objectSpread({
            className: "w-full border-gray-200 resize-none rounded-6px text-base leading-6 font-normal font-inter outline-none placeholder-gray-400",
            placeholder: "Type something",
            style: {
              minHeight: "138px"
            }
          }, field));
        }
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "flex justify-end mt-4",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
        type: "submit",
        className: "bg-dark-blue text-white rounded-6px py-3.5 px-6 disabled:bg-gray-200 disabled:text-gray-400 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500",
        disabled: false,
        value: "Create"
      })
    })]
  });
};

exports.CreateHypothesis = CreateHypothesis;