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

var _button = _interopRequireDefault(require("devextreme/ui/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; }

// require("./scss/application.scss");
// const updateComponentSignButton1 = (e, data) => {
//   props.data.sign?.map((s) => {
//   }
//   // e.component.option("disabled", true);
//   // let button = cButton.getInstance(document.getElementById("button" + data.id));
//   // console.log(button);
// };
var ComponentSign = function ComponentSign(props) {
  var _props$data$sign2;

  (0, _react.useEffect)(function () {
    var _loop = function _loop(index) {
      var _props$data$sign$inde;

      var button = _button["default"].getInstance(document.getElementById('button' + props.data.sign[index].id)); //Check if sign permission is null then enable the button unless its clicked already


      if (((_props$data$sign$inde = props.data.sign[index].sign_permission) === null || _props$data$sign$inde === void 0 ? void 0 : _props$data$sign$inde.data) === null) {
        var _button$element$attri;

        if (Boolean((_button$element$attri = button.element().attributes['clicked']) === null || _button$element$attri === void 0 ? void 0 : _button$element$attri.value) === true) {
          props.data.sign[index].disabled = true; //check if next level approved then disable undo and button

          var buttonsRelatedToThisSign = props.data.sign.find(function (s) {
            var _s$sign_permission, _s$sign_permission$da;

            return ((_s$sign_permission = s.sign_permission) === null || _s$sign_permission === void 0 ? void 0 : (_s$sign_permission$da = _s$sign_permission.data) === null || _s$sign_permission$da === void 0 ? void 0 : _s$sign_permission$da.id) === props.data.sign[index].id;
          });

          if (buttonsRelatedToThisSign) {
            for (var _index = 0; _index < buttonsRelatedToThisSign.length; _index++) {
              var dep = _button["default"].getInstance(document.getElementById('button' + buttonsRelatedToThisSign[_index].data.id));

              props.data.sign[_index].undoDisabled = false;

              if (dep) {
                var _dep$element$attribut;

                if (Boolean((_dep$element$attribut = dep.element().attributes['clicked']) === null || _dep$element$attribut === void 0 ? void 0 : _dep$element$attribut.value) === true) props.data.sign[_index].undoDisabled = true;
              }
            }
          }
        } else props.data.sign[index].disabled = false; // button.option('disabled', false);

      } else {
        var _props$data$sign$inde2, _props$data$sign$inde3, _dep$element$attribut2;

        var _dep = _button["default"].getInstance(document.getElementById('button' + ((_props$data$sign$inde2 = props.data.sign[index]) === null || _props$data$sign$inde2 === void 0 ? void 0 : (_props$data$sign$inde3 = _props$data$sign$inde2.sign_permission) === null || _props$data$sign$inde3 === void 0 ? void 0 : _props$data$sign$inde3.data.id)));

        if (Boolean((_dep$element$attribut2 = _dep.element().attributes['clicked']) === null || _dep$element$attribut2 === void 0 ? void 0 : _dep$element$attribut2.value) === true) button.option('disabled', false);else button.option('disabled', true);
      }
    };

    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;

      _loop(index);
    }
  }, [props]);

  var updateComponentSignButton = function updateComponentSignButton(e, item) {
    // if (!e) {
    //   props.data.sign.forEach((element) => {
    //     if (element.attributes.sign_permission.data?.id === item.id) {
    //       let checkbox = cBox.getInstance(
    //         document.getElementById("checkBox" + element.id)
    //       );
    //       checkbox.option("value", false);
    //     }
    //   });
    // }
    for (var index = 0; index < props.data.sign.length; index++) {
      var _props$data, _props$data$sign$inde4, _props$data$sign$inde5;

      var button = _button["default"].getInstance(document.getElementById('button' + ((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.sign[index].id)));

      if (((_props$data$sign$inde4 = props.data.sign[index]) === null || _props$data$sign$inde4 === void 0 ? void 0 : (_props$data$sign$inde5 = _props$data$sign$inde4.sign_permission) === null || _props$data$sign$inde5 === void 0 ? void 0 : _props$data$sign$inde5.data) != null) {
        var _props$data$sign$inde6;

        if (((_props$data$sign$inde6 = props.data.sign[index]) === null || _props$data$sign$inde6 === void 0 ? void 0 : _props$data$sign$inde6.sign_permission.data.id) === item.id) {
          button.option('disabled', false);
        } else {
          var _props$data$sign$inde7, _props$data$sign$inde8, _dep$element$attribut3;

          var dep = _button["default"].getInstance(document.getElementById('button' + ((_props$data$sign$inde7 = props.data.sign[index]) === null || _props$data$sign$inde7 === void 0 ? void 0 : (_props$data$sign$inde8 = _props$data$sign$inde7.sign_permission) === null || _props$data$sign$inde8 === void 0 ? void 0 : _props$data$sign$inde8.data.id)));

          if (Boolean((_dep$element$attribut3 = dep.element().attributes['clicked']) === null || _dep$element$attribut3 === void 0 ? void 0 : _dep$element$attribut3.value) === true) button.option('disabled', false);else button.option('disabled', true);
        }
      } // console.log(signPermissionData[index]);

    }
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "component-sign"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "CheckBoxField"
  }, (_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,
      user: props.user,
      onClick: updateComponentSignButton
    });
  })));
};

var _default = ComponentSign;
exports["default"] = _default;