Liferay.Loader.define("commerce-organization-web@4.0.20/js/components/MembersPane", ['module', 'exports', 'require', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../utils/constants', '../utils/utils', './MembersList', './PaneHeader'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _constants = require("../utils/constants");

    var _utils = require("../utils/utils");

    var _MembersList = _interopRequireDefault(require("./MembersList"));

    var _PaneHeader = _interopRequireDefault(require("./PaneHeader"));

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (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 _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    function _classCallCheck(instance, Constructor) {
      if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
      }
    }

    function _defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    function _createClass(Constructor, protoProps, staticProps) {
      if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
    }

    function _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      }return _assertThisInitialized(self);
    }

    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };return _getPrototypeOf(o);
    }

    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }return self;
    }

    function _inherits(subClass, superClass) {
      if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
      }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });if (superClass) _setPrototypeOf(subClass, superClass);
    }

    function _setPrototypeOf(o, p) {
      _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;return o;
      };return _setPrototypeOf(o, p);
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    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 USERS = _constants.LIST_BY.USERS;
    var membersListCopy = [];

    function fetchMembers(apiURL, orgId, listBy) {
      var q = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';

      var collectionPath = listBy + 's',
          apiParams = {
        baseURL: apiURL,
        id: orgId
      },
          apiParamsMembers = _objectSpread(_objectSpread({}, apiParams), {}, {
        path: collectionPath,
        queryParams: {
          page: 1,
          pageSize: 100,
          q: q
        }
      });

      return Promise.all([(0, _utils.callApi)(apiParams), (0, _utils.callApi)(apiParamsMembers)]).then(function (data) {
        var _data = _slicedToArray(data, 2),
            orgData = _data[0],
            membersData = _data[1];

        var accountsTotal = orgData.accountsTotal,
            orgName = orgData.name,
            totalSubOrg = orgData.organizationsTotal,
            usersTotal = orgData.usersTotal;
        return {
          id: orgId,
          members: membersData[collectionPath],
          orgName: orgName,
          totalAccounts: accountsTotal,
          totalSubOrg: totalSubOrg,
          totalUsers: usersTotal
        };
      });
    }

    function filterMembers(name, members) {
      return members.filter(function (member) {
        return member.name.toLowerCase().includes(name.toLowerCase());
      });
    }

    var MembersPane = /*#__PURE__*/function (_Component) {
      _inherits(MembersPane, _Component);

      function MembersPane(props) {
        var _this;

        _classCallCheck(this, MembersPane);

        _this = _possibleConstructorReturn(this, _getPrototypeOf(MembersPane).call(this, props));
        _this.state = {
          isLoading: true,
          listBy: USERS,
          searchQuery: ''
        };
        (0, _utils.bindAll)(_assertThisInitialized(_this), 'handleListBy', 'handleLookUp', 'handleUpdate');
        return _this;
      }

      _createClass(MembersPane, [{
        key: "componentDidMount",
        value: function componentDidMount() {
          var id = this.props.id;
          var listBy = this.state.listBy;
          this.$didUpdate = this.handleUpdate(id, listBy);
        }
      }, {
        key: "componentDidUpdate",
        value: function componentDidUpdate(prevProps, prevState) {
          var _this2 = this;

          var id = this.props.id;
          var listBy = this.state.listBy;

          if (id !== prevProps.id || listBy !== prevState.listBy) {
            this.setState(function () {
              return {
                isLoading: true
              };
            }, function () {
              _this2.$didUpdate = _this2.handleUpdate(id, listBy);
            });
          }
        }
      }, {
        key: "handleListBy",
        value: function handleListBy(listBy) {
          this.setState(function () {
            return {
              listBy: listBy
            };
          });
        }
      }, {
        key: "handleLookUp",
        value: function handleLookUp(e) {
          var _this3 = this;

          var name = e.target.value,
              _this$props = this.props,
              apiURL = _this$props.apiURL,
              id = _this$props.id,
              listBy = this.state.listBy,
              fromState = !!name && name.length ? filterMembers(name, this.state.members) : membersListCopy;

          if (fromState.length) {
            return new Promise(function (resolve) {
              _this3.setState(function () {
                return {
                  members: fromState
                };
              }, function () {
                resolve(true);
              });
            });
          } else {
            return fetchMembers(apiURL, id, listBy, name).then(function (_ref) {
              var total = _ref.total,
                  users = _ref.users;

              _this3.setState(function () {
                if (total) {
                  var fromFetch = filterMembers(name, users);
                  return fromFetch.length ? {
                    members: fromFetch
                  } : {
                    members: membersListCopy
                  };
                }

                return {
                  members: membersListCopy
                };
              });
            });
          }
        }
      }, {
        key: "handleUpdate",
        value: function handleUpdate(id, listBy) {
          var _this4 = this;

          var apiURL = this.props.apiURL;
          return fetchMembers(apiURL, id, listBy).then(function (data) {
            _this4.setState(function (state) {
              return Object.assign(state, data, {
                isLoading: false
              });
            }, function () {
              membersListCopy = _this4.state.members;
            });
          });
        }
      }, {
        key: "render",
        value: function render() {
          var _this$state = this.state,
              listBy = _this$state.listBy,
              members = _this$state.members,
              orgName = _this$state.orgName,
              totalAccounts = _this$state.totalAccounts,
              totalSubOrg = _this$state.totalSubOrg,
              totalUsers = _this$state.totalUsers;
          return (/*#__PURE__*/_react.default.createElement("div", {
              className: 'pane pane-open'
            }, /*#__PURE__*/_react.default.createElement(_PaneHeader.default, {
              colorIdentifier: this.props.colorIdentifier,
              listBy: this.state.listBy,
              onLookUp: this.handleLookUp,
              onViewSelected: this.handleListBy,
              orgName: orgName,
              spritemap: this.props.spritemap,
              totalAccounts: totalAccounts,
              totalSubOrg: totalSubOrg,
              totalUsers: totalUsers
            }), /*#__PURE__*/_react.default.createElement(_MembersList.default, {
              imagesPath: this.props.imagesPath,
              isLoading: this.state.isLoading,
              listBy: listBy,
              members: members,
              spritemap: this.props.spritemap
            }))
          );
        }
      }]);

      return MembersPane;
    }(_react.Component);

    _propTypes.default.defaultProps = {
      apiURL: '',
      selectedId: 0
    };
    var _default = MembersPane;
    exports.default = _default;
    //# sourceMappingURL=MembersPane.js.map
  }
});
//# sourceMappingURL=MembersPane.js.map