"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.PickerContentAdditionalStep = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _PickerContentBaseWrapped = require("./PickerContentBaseWrapped");
var _state = require("../../state");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _PickerContentAdditionalStepStCss = require("./PickerContentAdditionalStep.st.css.js");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/PickerContent/PickerContentAdditionalStep.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); }
const AdditionalStepContentRenderer = ({
  renderAdditionalStepContent,
  item
}) => (renderAdditionalStepContent == null ? void 0 : renderAdditionalStepContent({
  item
})) ?? null;
function _PickerContentAdditionalStep(props) {
  const {
    pickerContentState,
    renderItem,
    renderAdditionalStepContent,
    additionalStepTitle,
    additionalStepSubtitle,
    primaryButtonOnClick,
    secondaryButtonOnClick,
    onCloseButtonClick,
    onBeforeSelectAdditionalStep,
    onBackButtonClick,
    additionalStepOverrides = {},
    primaryButtonText,
    autoAdditionalStepOnInitialSelect,
    height,
    // extract from rest to disable overrides
    ...rest
  } = props;
  const [state] = (0, _react.useState)(() => pickerContentState.additionalStep = new _state.AdditionalStepState({
    collection: pickerContentState.picker.collection
  }));
  const selectedKeyedItem = state.selectedKeyedItem;
  const renderedItemProps = (0, _react.useMemo)(() => {
    if (!selectedKeyedItem) {
      return {
        title: '',
        subtitle: ''
      };
    }
    const {
      item,
      index
    } = selectedKeyedItem;
    const renderProps = renderItem(item, index);
    return {
      title: state.collection.itemName(item),
      ...renderProps
    };
  }, [selectedKeyedItem]);
  const {
    collection: {
      translate: t
    }
  } = state;
  const suffixNode = (0, _react.useMemo)(() => {
    return [additionalStepOverrides.extraNode, additionalStepOverrides.extraText, renderedItemProps.extraNode, renderedItemProps.extraText].find(node => node !== undefined);
  }, [renderedItemProps, additionalStepOverrides.extraNode, additionalStepOverrides.extraText]);
  renderedItemProps.image = (additionalStepOverrides == null ? void 0 : additionalStepOverrides.image) || renderedItemProps.image;
  renderedItemProps.subtitle = (additionalStepOverrides == null ? void 0 : additionalStepOverrides.subtitle) || renderedItemProps.subtitle;
  const enablePrimaryButton = (additionalStepOverrides == null ? void 0 : additionalStepOverrides.enablePrimaryButton) || false;
  return /*#__PURE__*/_react.default.createElement(_PickerContentBaseWrapped.PickerContentBaseWrapped, (0, _extends2.default)({}, rest, {
    primaryButtonOnClick: async () => {
      state.isLoading = true;
      if (onBeforeSelectAdditionalStep) {
        try {
          state.isLoading = true;
          state.additionalData = await (onBeforeSelectAdditionalStep == null ? void 0 : onBeforeSelectAdditionalStep());
          primaryButtonOnClick == null || primaryButtonOnClick();
        } catch (err) {
          console.error(err);
          state.isLoading = false;
        }
      } else {
        primaryButtonOnClick == null || primaryButtonOnClick();
      }
    },
    secondaryButtonOnClick: secondaryButtonOnClick,
    onCloseButtonClick: onCloseButtonClick,
    title: additionalStepTitle,
    subtitle: additionalStepSubtitle,
    primaryButtonText: state.isLoading ? /*#__PURE__*/_react.default.createElement(_designSystem.Loader, {
      size: "tiny",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 164,
        columnNumber: 27
      }
    }) : primaryButtonText,
    secondaryButtonText: t('cairo.cancel.button'),
    primaryButtonProps: {
      disabled: state.isLoading || !enablePrimaryButton
    },
    sideActions: autoAdditionalStepOnInitialSelect ? undefined : /*#__PURE__*/_react.default.createElement(_designSystem.TextButton, {
      prefixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ChevronLeft, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 173,
          columnNumber: 25
        }
      }),
      dataHook: "back-button",
      onClick: () => onBackButtonClick(),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 172,
        columnNumber: 11
      }
    }, t('cairo.pickerModal.back.button')),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 142,
      columnNumber: 5
    }
  }), /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    paddingLeft: "SP5",
    paddingRight: "SP5",
    paddingBottom: "SP4",
    gap: "SP4",
    overflow: "auto",
    height: "100%",
    dataHook: "additional-step-content",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 182,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 192,
      columnNumber: 9
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    gap: "SP3",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 193,
      columnNumber: 11
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    align: "center",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 194,
      columnNumber: 13
    }
  }, renderedItemProps.image), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _PickerContentAdditionalStepStCss.st)(_PickerContentAdditionalStepStCss.classes.titles),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 195,
      columnNumber: 13
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 196,
      columnNumber: 15
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Text, {
    dataHook: "title",
    ellipsis: true,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 197,
      columnNumber: 17
    }
  }, renderedItemProps.title)), /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 201,
      columnNumber: 15
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Text, {
    secondary: true,
    light: true,
    dataHook: "subtitle",
    ellipsis: true,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 202,
      columnNumber: 17
    }
  }, renderedItemProps.subtitle))), suffixNode !== undefined && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _PickerContentAdditionalStepStCss.st)(_PickerContentAdditionalStepStCss.classes.suffix),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 208,
      columnNumber: 15
    }
  }, typeof suffixNode === 'string' ? /*#__PURE__*/_react.default.createElement(_designSystem.Text, {
    secondary: true,
    dataHook: "suffix-text",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 210,
      columnNumber: 19
    }
  }, suffixNode) : /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    dataHook: "suffix-node",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 214,
      columnNumber: 19
    }
  }, suffixNode)))), /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    minHeight: "100px",
    width: "100%",
    dataHook: "additional-step-widget",
    position: "relative",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 220,
      columnNumber: 9
    }
  }, selectedKeyedItem && /*#__PURE__*/_react.default.createElement(AdditionalStepContentRenderer, {
    renderAdditionalStepContent: renderAdditionalStepContent,
    item: selectedKeyedItem.item,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 228,
      columnNumber: 13
    }
  }))));
}
const PickerContentAdditionalStep = exports.PickerContentAdditionalStep = (0, _mobxReactLite.observer)(_PickerContentAdditionalStep);
//# sourceMappingURL=PickerContentAdditionalStep.js.map