"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _findIndex = _interopRequireDefault(require("lodash/findIndex"));
var _get = _interopRequireDefault(require("lodash/get"));
var _clsx = _interopRequireDefault(require("clsx"));
var _styles = require("@material-ui/core/styles");
var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));
var _LoadingOverlay = _interopRequireDefault(require("../../../../../Framework/Components/LoadingOverlay"));
var _CustomDialog = _interopRequireDefault(require("../../../../../Shared/Components/CustomDialog"));
var _customClassName = _interopRequireDefault(require("../../../../Utils/customClassName"));
var _Option = _interopRequireDefault(require("../Option"));
var _excluded = ["render"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var useStyles = (0, _styles.makeStyles)(function (theme) {
  var _dialogStyle, _theme$breakpoints$be2, _ref;
  return _ref = {
    dialogStyle: (_dialogStyle = {
      height: '100%'
    }, _defineProperty(_dialogStyle, theme.breakpoints.down('sm'), {
      width: '100%',
      maxWidth: '390px'
    }), _defineProperty(_dialogStyle, theme.breakpoints.between('sm', 'xl'), {
      width: '470px',
      maxWidth: '470px'
    }), _defineProperty(_dialogStyle, theme.breakpoints.up('md'), {
      marginLeft: '120px',
      '& .MuiBackdrop-root': {
        left: '140px'
      }
    }), _dialogStyle)
  }, _defineProperty(_ref, theme.breakpoints.between('sm', 'md'), _defineProperty({}, theme.orientation.portrait, {
    top: '-110px !important'
  })), _defineProperty(_ref, '& > div.MuiDialog-container', _defineProperty({}, theme.breakpoints.between('sm', 'lg'), (_theme$breakpoints$be2 = {}, _defineProperty(_theme$breakpoints$be2, theme.orientation.portrait, {
    alignItems: 'flex-end'
  }), _defineProperty(_theme$breakpoints$be2, theme.orientation.landscape, {
    alignItems: 'flex-start',
    marginTop: 35
  }), _theme$breakpoints$be2))), _defineProperty(_ref, "contentStyle", {}), _defineProperty(_ref, "backDrop", {
    backgroundColor: 'rgba(0,0,0,0.0)'
  }), _ref;
}, {
  name: 'OptionsList'
});
var OptionsList = function OptionsList(_ref2) {
  var render = _ref2.render,
    props = _objectWithoutProperties(_ref2, _excluded);
  var addCheckedOptionclassification = props.addCheckedOptionclassification,
    children = props.children,
    isLoading = props.isLoading,
    onClose = props.onClose,
    onSelect = props.onSelect,
    onToggle = props.onToggle,
    optionclassification = props.optionclassification,
    options = props.options,
    show = props.show,
    viewDetails = props.viewDetails,
    viewStockInformation = props.viewStockInformation,
    LoadingComponent = props.LoadingComponent,
    OptionComponent = props.OptionComponent,
    isAdminMode = props.isAdminMode,
    setOptionInputText = props.setOptionInputText;
  var classes = useStyles();
  var createOptions = function createOptions() {
    var onOptionClicked = function onOptionClicked() {
      addCheckedOptionclassification(optionclassification);
    };
    return options.map(function (option) {
      var optionIndex = -1;
      var optionGroupChildren = (0, _get["default"])(option, 'option_group.children', []);
      if (optionGroupChildren.length === 0) {
        optionIndex = (0, _findIndex["default"])((0, _get["default"])(optionclassification, 'selectedoptions'), {
          identifier: option.identifier
        });
      } else {
        var _iterator = _createForOfIteratorHelper(optionGroupChildren),
          _step;
        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var item = _step.value;
            optionIndex = (0, _findIndex["default"])((0, _get["default"])(optionclassification, 'selectedoptions'), {
              identifier: item.identifier
            });
            if (optionIndex > -1) {
              // we need the first selected option
              break;
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      }
      var selected = optionIndex <= -1 ? false : !(option.amount_is_selectable && option.amount < 1);
      return /*#__PURE__*/_react["default"].createElement(OptionComponent, {
        key: 'optionlistitem' + option.identifier,
        option: option,
        optionclassification: optionclassification,
        onSelect: onSelect,
        selected: selected,
        viewDetails: viewDetails,
        viewStockInformation: viewStockInformation,
        onClick: onOptionClicked,
        onInputTextChange: setOptionInputText,
        onToggle: onToggle,
        isAdminMode: isAdminMode
      });
    });
  };
  var renderOptions = function renderOptions() {
    var optionListItems = createOptions();
    return /*#__PURE__*/_react["default"].createElement("div", null, children ? children : optionListItems.length > 0 ? optionListItems : null, /*#__PURE__*/_react["default"].createElement(LoadingComponent, {
      show: isLoading
    }));
  };
  var renderOptionsList = function renderOptionsList() {
    return /*#__PURE__*/_react["default"].createElement(_CustomDialog["default"], {
      className: (0, _clsx["default"])(classes.dialogStyle, (0, _customClassName["default"])('options')),
      dialogTitle: optionclassification.title,
      onCancel: onClose,
      onClose: onClose,
      open: show && !!optionclassification.identifier,
      BackdropProps: {
        classes: {
          root: classes.backDrop
        }
      }
    }, /*#__PURE__*/_react["default"].createElement(_DialogContent["default"], {
      className: classes.contentStyle
    }, renderOptions()));
  };
  var getRenderProps = function getRenderProps() {
    return _objectSpread(_objectSpread({}, props), {}, {
      renderOptions: renderOptions,
      createOptions: createOptions,
      renderOptionsList: renderOptionsList
    });
  };
  return typeof render === 'function' ? render(getRenderProps()) : renderOptionsList();
};
OptionsList.defaultProps = {
  addCheckedOptionclassification: function addCheckedOptionclassification() {},
  isLoading: false,
  onClose: function onClose() {},
  onSelect: function onSelect() {},
  onToggle: function onToggle() {},
  options: [],
  optionclassification: {},
  show: false,
  viewDetails: function viewDetails() {},
  viewStockInformation: function viewStockInformation() {},
  LoadingComponent: _LoadingOverlay["default"],
  OptionComponent: _Option["default"]
};
var _default = exports["default"] = OptionsList;