"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _Dialog = _interopRequireDefault(require("../../Framework/Components/Dialog"));
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));
var _LoadingOverlay = _interopRequireDefault(require("../../Framework/Components/LoadingOverlay"));
var _styles = require("@material-ui/core/styles");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": 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; }
var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    content: {
      padding: '0 !important'
    },
    labels: {},
    option: {
      minHeight: 25,
      display: 'table',
      width: '100%',
      '& $labels': {
        display: 'table-cell',
        verticalAlign: 'middle',
        padding: '0px 5px',
        '& $price': {
          "float": 'right'
        }
      },
      '&.selected': {
        backgroundColor: theme.palette.secondary.lighter
      }
    },
    price: {},
    root: {
      '& .dialog-title-bar': {
        display: 'none'
      }
    }
  };
}, {
  name: 'CustomDialogSelect'
});
var CustomDialogSelect = function CustomDialogSelect(_ref) {
  var isLoading = _ref.isLoading,
    isSelected = _ref.isSelected,
    onSelectOption = _ref.onSelectOption,
    options = _ref.options,
    show = _ref.show,
    selectedOption = _ref.selectedOption,
    selectedOptionProperty = _ref.selectedOptionProperty,
    LoadingComponent = _ref.LoadingComponent;
  var classes = useStyles();
  var isOptionSelected = function isOptionSelected(option) {
    if (isSelected) {
      return isSelected(option);
    }
    if (!selectedOption) {
      return false;
    }

    // if there are more values in the selection
    if ((0, _isArray["default"])(selectedOption)) {
      var _iterator = _createForOfIteratorHelper(selectedOption),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var v = _step.value;
          if (selectedOptionProperty) {
            if (v[selectedOptionProperty] === option[selectedOptionProperty]) {
              return true;
            }
          } else {
            if (v === option) {
              return true;
            }
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    } else if (selectedOptionProperty) {
      return selectedOption[selectedOptionProperty] === option[selectedOptionProperty];
    } else {
      return selectedOption === option;
    }
  };
  var renderOption = function renderOption(option, key) {
    var selected = isOptionSelected(option);
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: classes.option + (selected ? ' selected' : ''),
      key: key,
      "data-option": option.identifier,
      onClick: function onClick() {
        return onSelectOption(option);
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: classes.labels
    }, /*#__PURE__*/_react["default"].createElement("span", null, option.title), /*#__PURE__*/_react["default"].createElement("span", {
      className: classes.price
    }, option.priceFormatted)));
  };
  var renderComponent = function renderComponent() {
    return /*#__PURE__*/_react["default"].createElement(_Dialog["default"], {
      className: classes.root,
      show: show
    }, /*#__PURE__*/_react["default"].createElement(_DialogContent["default"], {
      className: classes.content
    }, isLoading ? /*#__PURE__*/_react["default"].createElement(LoadingComponent, {
      show: isLoading
    }) : (0, _isArray["default"])(options) && options.map(function (option, key) {
      return renderOption(option, key);
    })));
  };
  return renderComponent();
};
CustomDialogSelect.defaultProps = {
  onSelectOption: function onSelectOption() {},
  options: [],
  show: false,
  LoadingComponent: _LoadingOverlay["default"]
};
var _default = exports["default"] = CustomDialogSelect;