"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _componentSignButton = _interopRequireDefault(require("./component-sign-button"));
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 && 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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var ComponentSign = function ComponentSign(props) {
  var _props$data$sign2;
  //const [updated, setUpdated] = useState(null);

  (0, _react.useEffect)(function () {
    updateSignDisable();
  }, []);
  var updateSignDisable = function updateSignDisable() {
    var _loop = function _loop(index) {
        var buttonsRelatedToThisSign = props.data.sign.filter(function (s) {
          var _s$attributes$sign_pe;
          return ((_s$attributes$sign_pe = s.attributes.sign_permission) === null || _s$attributes$sign_pe === void 0 || (_s$attributes$sign_pe = _s$attributes$sign_pe.data) === null || _s$attributes$sign_pe === void 0 ? void 0 : _s$attributes$sign_pe.id) === props.data.sign[index].id;
        });
        if (!props.mutable) {
          props.data.sign[index].disabled = true;
          props.data.sign[index].undoDisabled = false;
          if (buttonsRelatedToThisSign) {
            var _iterator = _createForOfIteratorHelper(buttonsRelatedToThisSign),
              _step;
            try {
              for (_iterator.s(); !(_step = _iterator.n()).done;) {
                var element = _step.value;
                if (element.clicked === true) props.data.sign[index].undoDisabled = true;
              }
            } catch (err) {
              _iterator.e(err);
            } finally {
              _iterator.f();
            }
          }
        } else {
          var _props$data$sign$inde;
          if (props.data.sign[index].clicked === undefined) props.data.sign[index].clicked = false;
          if (((_props$data$sign$inde = props.data.sign[index].attributes.sign_permission) === null || _props$data$sign$inde === void 0 ? void 0 : _props$data$sign$inde.data) === null) {
            //Check if sign permission is null then enable the button unless its clicked already
            props.data.sign[index].disabled = false;
            props.data.sign[index].showReject = false;
            if (props.data.sign[index].clicked === true) {
              props.data.sign[index].disabled = true;
              //check if next level approved then disable undo and button
              props.data.sign[index].undoDisabled = false;
              if (buttonsRelatedToThisSign) {
                var _iterator2 = _createForOfIteratorHelper(buttonsRelatedToThisSign),
                  _step2;
                try {
                  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
                    var _element = _step2.value;
                    if (_element.clicked === true) props.data.sign[index].undoDisabled = true;
                  }
                } catch (err) {
                  _iterator2.e(err);
                } finally {
                  _iterator2.f();
                }
              }
            }
            if (props.data.sign[index].disabled || props.data.sign[index].clicked) {
              props.data.sign[index].disabled = true;
              return 0; // continue
            }
            if (!(props.data.sign[index].attributes.permissions && props.data.sign[index].attributes.permissions.findIndex(function (r) {
              return r.key === props.data.user.role.id;
            }) >= 0 || props.data.sign[index].attributes.permissions === undefined || props.data.sign[index].attributes.permissions.length === 0)) props.data.sign[index].disabled = true;else {
              props.data.sign[index].disabled = false;
              props.data.sign[index].showReject = false;
            }
          } else {
            props.data.sign[index].disabled = false;
            props.data.sign[index].showReject = true;
            var depSign = props.data.sign.find(function (s) {
              var _props$data$sign$inde2;
              return s.id === ((_props$data$sign$inde2 = props.data.sign[index]) === null || _props$data$sign$inde2 === void 0 || (_props$data$sign$inde2 = _props$data$sign$inde2.attributes.sign_permission) === null || _props$data$sign$inde2 === void 0 ? void 0 : _props$data$sign$inde2.data.id);
            });
            //console.log('depSign', depSign.clicked);
            if (depSign && depSign.clicked === true) {
              props.data.sign[index].disabled = false;
              props.data.sign[index].showReject = true;
            } else props.data.sign[index].disabled = true;
            if (props.data.sign[index].disabled || props.data.sign[index].clicked) {
              props.data.sign[index].disabled = true;
              return 0; // continue
            }
            if (!(props.data.sign[index].attributes.permissions && props.data.sign[index].attributes.permissions.findIndex(function (r) {
              return r.key === props.data.user.role.id;
            }) >= 0 || props.data.sign[index].attributes.permissions === null || props.data.sign[index].attributes.permissions === undefined || props.data.sign[index].attributes.permissions.length === 0)) props.data.sign[index].disabled = true;else {
              props.data.sign[index].disabled = false;
              props.data.sign[index].showReject = true;
            }
            // console.log('props.data.sign', props.data.sign);
            if (buttonsRelatedToThisSign) {
              var _iterator3 = _createForOfIteratorHelper(buttonsRelatedToThisSign),
                _step3;
              try {
                for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
                  var _element2 = _step3.value;
                  if (_element2.clicked === true) props.data.sign[index].undoDisabled = true;
                }
              } catch (err) {
                _iterator3.e(err);
              } finally {
                _iterator3.f();
              }
            }
          }
        }
      },
      _ret;
    // console.log('props.data.sign', props.data);
    for (var index = 0; index < ((_props$data$sign = props.data.sign) === null || _props$data$sign === void 0 ? void 0 : _props$data$sign.length); index++) {
      var _props$data$sign;
      _ret = _loop(index);
      if (_ret === 0) continue;
    }
    // props.updatePropsData(item);
    // setUpdated(Date.now());
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "component-sign"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "CheckBoxField"
  }, props.data.sign && ((_props$data$sign2 = props.data.sign) === null || _props$data$sign2 === void 0 ? void 0 : _props$data$sign2.map(function (s) {
    return /*#__PURE__*/_react["default"].createElement(_componentSignButton["default"], {
      key: "ComponentSignButton".concat(s.id),
      data: s,
      parentId: props.data.id,
      readOnly: props.data.read_only,
      onClick: props.data.onSignClicked ? props.data.onSignClicked : null,
      updatePropsData: updateSignDisable,
      mutable: props.mutable
    });
  }))));
};
var _default = exports["default"] = ComponentSign;