import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { u as useViewHandler, _ as _onClickCheckout, I as Icon } from './index-bbdfb97a.js';
import { useState, useEffect } from 'react';
import { r as roomItem, s as sel, b as buttonVisible, g as generated, l as loading, w as wrapper, t as textShadow, a as title, c as button, d as limitedButton, e as stopButton, f as blockedButton, h as limitedIcon, i as loadingIcon, j as generateIcon, k as favoriteIcon, m as favoriteIconSel, n as favoriteIconVisible, o as shareButtonWrapper, v as visible } from './index.module-613d19d3.js';
import classnames from 'classnames';
import { observer } from 'mobx-react-lite';
import InfoIcon from './index-04a7a9b4.js';
import { useTranslation, useDivaCore } from '@crystaldesign/diva-core';
import { jsxs, jsx } from 'react/jsx-runtime';
import 'react-dynamic-multi-render';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/regenerator';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/toConsumableArray';
import '@crystaldesign/diva-utils';
import 'mobx';
import '@crystaldesign/web-planner-logic';
import 'qrcode.react';
import '@babel/runtime/helpers/typeof';
import 'flat';
import 'react-switch';
import 'react-countup';

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; }
var RoomItem = function RoomItem(_ref) {
  var _viewHandler$uiState$, _viewHandler$aiState$5, _viewHandler$aiState$6, _ref2, _viewHandler$settings, _viewHandler$aiState$7;
  var item = _ref.item;
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var viewHandler = useViewHandler();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    generated$1 = _useState2[0],
    setGenerated = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading$1 = _useState4[0],
    setLoading = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    favorite = _useState6[0],
    setFavorite = _useState6[1];
  var _useDivaCore = useDivaCore(),
    user = _useDivaCore.state.user;

  // Get limit status from AI state instead of local state

  var onButtonClick = function onButtonClick() {
    if (loading$1) return;
    viewHandler.aiState.handleGenerate(item.key);
  };
  var onItemClick = function onItemClick() {
    var _viewHandler$aiState$;
    var hash = viewHandler.aiState.setHash;
    if ((_viewHandler$aiState$ = viewHandler.aiState.aiGeneratedRooms[item.key]) !== null && _viewHandler$aiState$ !== void 0 && _viewHandler$aiState$[hash]) {
      var _item$onClick;
      viewHandler.aiState.useAiGeneratedRoom(item.key, hash);
      (_item$onClick = item.onClick) === null || _item$onClick === void 0 || _item$onClick.call(item);
    }
  };
  useEffect(function () {
    var _viewHandler$aiState$2, _viewHandler$aiState$3, _viewHandler$aiState$4;
    var hash = viewHandler.aiState.setHash;
    setGenerated(((_viewHandler$aiState$2 = viewHandler.aiState.aiGeneratedRooms[item.key]) === null || _viewHandler$aiState$2 === void 0 || (_viewHandler$aiState$2 = _viewHandler$aiState$2[hash]) === null || _viewHandler$aiState$2 === void 0 ? void 0 : _viewHandler$aiState$2.image) !== undefined);
    setLoading(((_viewHandler$aiState$3 = viewHandler.aiState.aiGeneratedRooms[item.key]) === null || _viewHandler$aiState$3 === void 0 || (_viewHandler$aiState$3 = _viewHandler$aiState$3[hash]) === null || _viewHandler$aiState$3 === void 0 ? void 0 : _viewHandler$aiState$3.promise) && ((_viewHandler$aiState$4 = viewHandler.aiState.aiGeneratedRooms[item.key]) === null || _viewHandler$aiState$4 === void 0 || (_viewHandler$aiState$4 = _viewHandler$aiState$4[hash]) === null || _viewHandler$aiState$4 === void 0 ? void 0 : _viewHandler$aiState$4.promise) !== 'fulfilled');
    setFavorite(!!viewHandler.aiState.favoriteAiImageKeys.find(function (f) {
      return f.key === item.key;
    }));
  }, [viewHandler.aiState.setHash, viewHandler.aiState.aiGeneratedRooms, viewHandler.aiState.favoriteAiImageKeys]);
  var isAnonymous = !user;
  var shareButton = (_viewHandler$uiState$ = viewHandler.uiState.checkoutConfig) === null || _viewHandler$uiState$ === void 0 ? void 0 : _viewHandler$uiState$.buttons.find(function (b) {
    return b.type === 'SHARE';
  });
  var isLimitReached = (_viewHandler$aiState$5 = (_viewHandler$aiState$6 = viewHandler.aiState.usageBudget) === null || _viewHandler$aiState$6 === void 0 ? void 0 : _viewHandler$aiState$6.isLimitReached) !== null && _viewHandler$aiState$5 !== void 0 ? _viewHandler$aiState$5 : false;
  var canGetLeadRewardBonus = (_ref2 = ((_viewHandler$settings = viewHandler.settingsState.organizationAISettings) === null || _viewHandler$settings === void 0 ? void 0 : _viewHandler$settings.leadRewardBonus) && !((_viewHandler$aiState$7 = viewHandler.aiState.usageBudget) !== null && _viewHandler$aiState$7 !== void 0 && _viewHandler$aiState$7.leadKeyActive) && isAnonymous) !== null && _ref2 !== void 0 ? _ref2 : false;

  // For anonymous users, disable all buttons when any generation is in progress
  var isAnyGenerationInProgress = viewHandler.aiState.isAnyGenerationInProgress;
  var blockedWaitingForOtherGeneration = isAnonymous && isAnyGenerationInProgress && !loading$1;
  return /*#__PURE__*/jsxs("div", {
    className: classnames(roomItem, item.selected ? sel : undefined, !generated$1 ? buttonVisible : generated, loading$1 ? loading : undefined),
    onClick: onItemClick,
    children: [/*#__PURE__*/jsxs("div", {
      className: wrapper,
      children: [/*#__PURE__*/jsx("img", {
        src: item.image
      }), /*#__PURE__*/jsx("div", {
        className: textShadow
      }), /*#__PURE__*/jsx("div", {
        className: title,
        children: item.name
      }), item.contentItemId && /*#__PURE__*/jsx(InfoIcon, {
        item: item,
        style: {
          top: 0,
          bottom: 'unset'
        }
      })]
    }), /*#__PURE__*/jsxs("button", {
      className: classnames(button, isLimitReached ? limitedButton : undefined, !canGetLeadRewardBonus && isLimitReached ? stopButton : undefined, blockedWaitingForOtherGeneration && !loading$1 ? blockedButton : undefined),
      onClick: onButtonClick,
      disabled: isLimitReached && !canGetLeadRewardBonus || blockedWaitingForOtherGeneration,
      children: [/*#__PURE__*/jsx("div", {
        className: classnames(isLimitReached ? limitedIcon : loading$1 ? loadingIcon : generateIcon)
      }), isLimitReached ? canGetLeadRewardBonus ? t('configurator.roomRendering.limited') : t('configurator.roomRendering.stop') : loading$1 ? t('configurator.roomRendering.generating') : blockedWaitingForOtherGeneration ? t('configurator.roomRendering.blockedWaitingForOtherGeneration') : t('configurator.roomRendering.generate')]
    }), /*#__PURE__*/jsx("div", {
      className: classnames(favoriteIcon, favorite ? favoriteIconSel : undefined, generated$1 && (item.selected || favorite) ? favoriteIconVisible : undefined),
      title: t('configurator.roomRendering.includeInBasket'),
      onClick: function onClick() {
        return viewHandler.aiState.onFavoriteAiImageKeyChange(item.key, false);
      }
    }), shareButton && !viewHandler.uiState.isInMobileView && /*#__PURE__*/jsx("div", {
      className: classnames(shareButtonWrapper, generated$1 && item.selected ? visible : undefined),
      onClick: function onClick() {
        viewHandler.divaWebPlanner.setAiImageAsJpg(item.key, item.image);
        _onClickCheckout(shareButton, viewHandler, shareButton.index, t);
      },
      children: /*#__PURE__*/jsx(Icon, {
        checkoutButton: _objectSpread(_objectSpread({}, shareButton), {}, {
          isPrimary: true
        }),
        size: 28,
        showTitle: false
      })
    }, shareButton.index)]
  });
};
var RoomItem$1 = observer(RoomItem);

export { RoomItem$1 as default };
