"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 _react = _interopRequireWildcard(require("react"));
var _mobx = require("mobx");
var _usePickerContent = require("../../hooks/usePickerContent");
var _state = require("../../state");
var _PickerContentAdditionalStep = require("./PickerContentAdditionalStep");
var _PickerContentItemsSelection = require("./PickerContentItemsSelection");
var _PickerContentBaseWrapped = require("./PickerContentBaseWrapped");
var _MaxSelection = require("../MaxSelection");
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/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 [state] = (0, _react.useState)(() => params instanceof _state.PickerContentState ? params : createPickerContentState(params));
  (0, _react.useEffect)(() => {
    return state.init({
      autoAdditionalStepOnInitialSelect: props.autoAdditionalStepOnInitialSelect
    });
  }, [state]);
  const {
    isAdditionalStepShown,
    setIsAdditionalStepShown,
    isAutoAdditionalStepPending
  } = state;
  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, props == null ? void 0 : props.hasAdditionalStep]);
  if (isAutoAdditionalStepPending) {
    return /*#__PURE__*/_react.default.createElement(_PickerContentBaseWrapped.PickerContentBaseWrapped, {
      dataHook: props.dataHook,
      width: props.width,
      secondaryButtonText: t('cairo.cancel.button'),
      secondaryButtonOnClick: secondaryOnClick,
      onCloseButtonClick: onCloseButtonClick,
      primaryButtonProps: {
        disabled: true
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 110,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
      height: "100%",
      minHeight: "300px",
      align: "center",
      verticalAlign: "middle",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 118,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Loader, {
      dataHook: "auto-additional-step-loader",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 124,
        columnNumber: 11
      }
    })));
  }
  return isAdditionalStepShown ? /*#__PURE__*/_react.default.createElement(_PickerContentAdditionalStep.PickerContentAdditionalStep, (0, _extends2.default)({}, props, {
    pickerContentState: state,
    onBackButtonClick: () => {
      picker.onBackButtonClick();
      setIsAdditionalStepShown(false);
      backButtonOnClick == null || backButtonOnClick({
        item: selectedValue
      });
    },
    primaryButtonOnClick: () => {
      primaryOnClick({
        currentLevel: 2,
        numLevels: 2
      });
    },
    secondaryButtonOnClick: secondaryOnClick,
    onCloseButtonClick: onCloseButtonClick,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 131,
      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 && selectedValue) {
        picker.onContinueButtonClick();
        setIsAdditionalStepShown(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: 146,
      columnNumber: 5
    }
  }));
}
const PickerContentWrapped = exports.PickerContentWrapped = (0, _mobxReactLite.observer)(_PickerContentWrapped);
//# sourceMappingURL=PickerContentWrapped.js.map