"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _componentSignButton = _interopRequireDefault(require("./component-sign-button"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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;
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    updated = _useState2[0],
    setUpdated = _useState2[1];
  (0, _react.useEffect)(function () {
    updateSignDisable();
  }, []);
  var updateSignDisable = function updateSignDisable() {
    var _loop = function _loop(index) {
        var _props$data$sign$inde;
        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.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;
          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 _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();
              }
            }
          }
          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;
        } else {
          props.data.sign[index].disabled = false;
          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;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 === undefined || props.data.sign[index].attributes.permissions.length === 0)) props.data.sign[index].disabled = true;else props.data.sign[index].disabled = false;
          // console.log('props.data.sign', props.data.sign);
          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();
            }
          }
        }
      },
      _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"
  }, updated ? '' : '', /*#__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 = ComponentSign;
exports["default"] = _default;