"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.PickerContentWrapped = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _mobx = require("mobx");
var _react = _interopRequireWildcard(require("react"));
var _usePickerContent = require("../../hooks/usePickerContent");
var _state = require("../../state");
var _PickerContentAdditionalStep = require("./PickerContentAdditionalStep");
var _PickerContentItemsSelection = require("./PickerContentItemsSelection");
var _MaxSelection = require("../MaxSelection");
var _mobxReactLite = require("mobx-react-lite");
var _jsxFileName = "/home/builduser/work/db7ea24852bc3350/packages/cairo/dist/cjs/components/PickerContent/PickerContentWrapped.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _PickerContentWrapped(props) {
  var _picker$collection$bu;
  const {
    state: params,
    renderPickerContent,
    primaryButtonOnClick,
    backButtonOnClick,
    continueButtonOnClick
  } = props;
  const createPickerContentState = (0, _usePickerContent.useCreatePickerContentState)();
  const [isAdditionalStepShown, showAdditionalStep] = (0, _react.useState)(false);
  const [state] = (0, _react.useState)(() => params instanceof _state.PickerContentState ? params : createPickerContentState(params));
  const {
    picker,
    modalState
  } = state;
  const {
    _isModalOpen
  } = modalState;
  const primaryOnClick = ({
    currentLevel,
    numLevels
  }) => {
    picker.onPrimaryButtonClick({
      currentLevel,
      numLevels
    });
    (0, _mobx.runInAction)(() => {
      var _state$additionalStep;
      modalState.onPrimaryButtonClick(state, (_state$additionalStep = state.additionalStep) == null ? void 0 : _state$additionalStep.additionalData);
    });
    primaryButtonOnClick == null || primaryButtonOnClick();
  };
  const secondaryOnClick = () => {
    picker.onSecondaryButtonOnClick();
    _isModalOpen.value = false;
    props == null || props.secondaryButtonOnClick == null || props.secondaryButtonOnClick();
  };
  const onCloseButtonClick = event => {
    picker.onCloseButtonClick();
    _isModalOpen.value = false;
    props == null || props.onCloseButtonClick == null || props.onCloseButtonClick(event);
  };
  const {
    collection: {
      translate: t
    }
  } = picker;
  const selectedValue = (_picker$collection$bu = picker.collection.bulkSelect.selectedValues) == null ? void 0 : _picker$collection$bu[0];
  const hasAdditionalStep = _react.default.useMemo(() => selectedValue && (props == null || props.hasAdditionalStep == null ? void 0 : props.hasAdditionalStep({
    item: selectedValue
  })), [selectedValue]);
  return isAdditionalStepShown ? /*#__PURE__*/_react.default.createElement(_PickerContentAdditionalStep.PickerContentAdditionalStep, (0, _extends2.default)({}, props, {
    pickerContentState: state,
    onBackButtonClick: () => {
      picker.onBackButtonClick();
      showAdditionalStep(false);
      backButtonOnClick == null || backButtonOnClick({
        item: selectedValue
      });
    },
    primaryButtonOnClick: () => {
      primaryOnClick({
        currentLevel: 2,
        numLevels: 2
      });
    },
    secondaryButtonOnClick: secondaryOnClick,
    onCloseButtonClick: onCloseButtonClick,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 90,
      columnNumber: 5
    }
  })) : /*#__PURE__*/_react.default.createElement(_PickerContentItemsSelection.PickerContentItemsSelection, (0, _extends2.default)({}, props, {
    state: state,
    primaryButtonOnClick: () => {
      if (
      // currently additional step supports only single selection
      !(0, _MaxSelection.fromMultipleSelectionToBoolean)(props == null ? void 0 : props.multiple) && hasAdditionalStep) {
        picker.onContinueButtonClick();
        showAdditionalStep(true);
        continueButtonOnClick == null || continueButtonOnClick({
          item: selectedValue
        });
      } else {
        primaryOnClick({
          currentLevel: 1,
          numLevels: 1
        });
      }
    },
    primaryButtonText: hasAdditionalStep ? t('cairo.continue') : props.primaryButtonText || t('cairo.continue'),
    secondaryButtonOnClick: secondaryOnClick,
    renderPickerContent: renderPickerContent,
    onCloseButtonClick: onCloseButtonClick,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 105,
      columnNumber: 5
    }
  }));
}
const PickerContentWrapped = exports.PickerContentWrapped = (0, _mobxReactLite.observer)(_PickerContentWrapped);
//# sourceMappingURL=PickerContentWrapped.js.map