import React, { useMemo, useContext } from "react";
import { Context } from "../../model";
import { Button } from "antd";
import { getIsEdit } from "../../../utils";
import { isUsableProduct } from "../addTimeModal/utils";
import "./index.less";
import { locales } from "@pisell/utils";
var Actions = function Actions(_ref) {
  var _state$bookingConfig2;
  var currentService = _ref.currentService;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var appointment_card = useMemo(function () {
    var _state$bookingConfig;
    return (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.information_tab) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_card) || {
      quick_time: 1
    };
  }, [state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.information_tab]);
  var onAddTime = function onAddTime() {
    dispatch({
      type: "setService",
      payload: {
        addTimeModal: true,
        cacheItem: currentService
      }
    });
  };
  var addTimeProducts = useMemo(function () {
    return (state.addTimeProducts || []).some(function (item) {
      return isUsableProduct(item.add_time, currentService);
    });
  }, [state.addTimeProducts, currentService]);
  var showAddTimeButton = useMemo(function () {
    // event预约不需要
    if (state.renderType === "eventBooking") {
      return false;
    }

    // 禁止编辑
    if (state.disabledEdit) {
      return false;
    }

    // 未开启加时
    if (appointment_card.quick_time !== 1) {
      return false;
    }

    // 如果在编辑态, 不展示加时
    if (getIsEdit(state)) {
      return false;
    }

    // 如果该服务没有符合的加时商品, 不展示加时
    if (!addTimeProducts) {
      return false;
    }
    return true;
  }, [state, appointment_card.quick_time, addTimeProducts]);
  return /*#__PURE__*/React.createElement("div", {
    className: "actions-wrap"
  }, showAddTimeButton && /*#__PURE__*/React.createElement(Button, {
    className: "action-item",
    size: "large",
    onClick: function onClick(e) {
      e.preventDefault();
      e.stopPropagation();
      onAddTime();
    }
  }, locales.getText("page.booking.text.add-time")));
};
export default Actions;