var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/info/header/index.tsx
var header_exports = {};
__export(header_exports, {
  default: () => header_default
});
module.exports = __toCommonJS(header_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_icons = require("@ant-design/icons");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var import_classnames = __toESM(require("classnames"));
var header = ({ state, dispatch }) => {
  var _a, _b, _c;
  const context = (0, import_useEngineContext.default)();
  const [modalContent, setModalContent] = (0, import_react.useState)({
    open: false,
    children: "",
    footer: null,
    title: /* @__PURE__ */ import_react.default.createElement("div", { className: "", style: { height: 14 } })
  });
  const { isTerminal, interaction, store, storage } = (_a = context.appHelper) == null ? void 0 : _a.utils;
  const bookingDetail = (0, import_react.useMemo)(() => {
    return {
      ...state.preliminaryBookingDetail || {},
      ...state.bookingDetail || {}
    };
  }, [state.bookingDetail, state.preliminaryBookingDetail]);
  const statusList = [
    {
      label: import_utils.locales.getText("pisell2.text.status.new"),
      key: "new",
      color: "#91caff"
    },
    {
      label: import_utils.locales.getText("pisell2.text.status.confirmed"),
      key: "confirmed",
      color: "#b7eb8f"
    },
    {
      label: import_utils.locales.getText("pisell2.text.status.arrived"),
      key: "arrived",
      color: "#d3adf7"
    },
    {
      label: import_utils.locales.getText("pisell2.text.status.started"),
      key: "started",
      color: "#fffb8f"
    },
    {
      label: /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#D92D20" } }, import_utils.locales.getText("pisell2.text.status.cancelled")),
      key: "cancelled",
      color: "#bfbfbf"
    },
    {
      label: /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#D92D20" } }, import_utils.locales.getText("pisell2.text.status.no_show")),
      key: "no_show",
      color: "#ffa39e"
    },
    {
      label: import_utils.locales.getText("pisell2.text.status.completed"),
      key: "completed",
      color: "#ffd591"
    }
  ];
  const orderSalesChannel = {
    online_store: import_utils.locales.getText("pisell2.text.order.sales.channel.online"),
    pos: import_utils.locales.getText("pisell2.text.order.sales.channel.pos-terminal"),
    boxlocal: import_utils.locales.getText("pisell2.text.order.sales.channel.boxcocal"),
    "table-order": import_utils.locales.getText(
      "pisell2.text.order.sales.channel.tableorder"
    ),
    google_reserve: import_utils.locales.getText("pisell2.text.order.sales.channel.reserve")
  };
  const getStatusMap = () => {
    let obj = {};
    for (let i = 0; i < statusList.length; i++) {
      obj[statusList[i].key] = statusList[i];
    }
    return obj;
  };
  const statusMps = getStatusMap();
  const items = (0, import_react.useMemo)(() => {
    return statusList.filter((item) => item.key !== state.appointment_status);
  }, [statusList]);
  const currentStatus = (0, import_react.useMemo)(() => {
    let status = state.appointment_status;
    return statusMps[status] || statusMps["new"];
  }, [state.appointment_status]);
  const setBookingStatus = async (status) => {
    dispatch({
      type: "setStatus",
      payload: status
    });
    const data = await state.apis.editBookingStatus({
      appointment_status: status
    });
    import_antd.message.success(import_utils.locales.getText("pisell1.text.edit-status-success"));
    state == null ? void 0 : state.callback({ order_id: state.bookingId });
  };
  const onSelect = async (e) => {
    if (e.key === "cancelled") {
      handleCancel();
      return;
    }
    await setBookingStatus(e.key);
  };
  const setModalContentState = (values) => {
    setModalContent({ ...modalContent, ...values });
  };
  const goCheckout = async () => {
    var _a2, _b2;
    if (isTerminal == null ? void 0 : isTerminal()) {
      return (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.postMessageToApp) == null ? void 0 : _b2.call(_a2, {
        module: "booking",
        key: "booking_checkout",
        data: {
          order_id: state.bookingId,
          callbackType: "pisell2"
        }
      });
    }
    state.action({
      type: "pisell1.handlePayment",
      data: {
        orderId: state.bookingId
      },
      callback: () => {
        state == null ? void 0 : state.callback();
      }
    });
  };
  const isPaid = (0, import_react.useMemo)(() => {
    return (bookingDetail == null ? void 0 : bookingDetail.payment_status) === "paid";
  }, [bookingDetail == null ? void 0 : bookingDetail.payment_status]);
  const isTicket = (0, import_react.useMemo)(() => {
    var _a2;
    return (((_a2 = state.service) == null ? void 0 : _a2.value) || []).some(
      (d) => ["appointment_ticket", "product_package_ticket", "ticket"].includes(
        d.extension_type
      )
    );
  }, [(_b = state.service) == null ? void 0 : _b.value]);
  const paymentProcessingPayment = (type) => {
    const payments = (bookingDetail == null ? void 0 : bookingDetail.payments) || [];
    const status = ["payment_processing"];
    const payment_method = ["BANK_DEPOSIT"];
    if (type === "bank") {
      return !!(payments == null ? void 0 : payments.find(
        (item) => status.includes(item == null ? void 0 : item.status) && payment_method.includes(item == null ? void 0 : item.payment_method)
      ));
    }
    return !!(payments == null ? void 0 : payments.find((item) => status.includes(item == null ? void 0 : item.status)));
  };
  const handleCancel = () => {
    if (paymentProcessingPayment("bank")) {
      return showPaymentModal();
    }
    if (paymentProcessingPayment()) {
      return showCancelCalendarModal();
    }
    return showCancelledModal();
  };
  const handleRefund = () => {
    var _a2, _b2;
    if (isTerminal == null ? void 0 : isTerminal()) {
      return (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.postMessageToApp) == null ? void 0 : _b2.call(_a2, {
        module: "booking",
        key: "booking_refund",
        data: {
          order_id: state.bookingId
        },
        origin: "/booking"
      });
    }
    state.action({
      type: "pisell1.handleRefund",
      data: {
        orderId: state.bookingId
      },
      callback: () => {
        state == null ? void 0 : state.callback({ order_id: state.bookingId });
      }
    });
  };
  const showPaymentModal = () => {
    setModalContentState({
      className: "go-payments-modal-wrap",
      open: true,
      width: 480,
      onCancel: () => {
        setModalContentState({ open: false });
      },
      children: /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content-title" }, import_utils.locales.getText("page.booking.text.unconfirmed-payments")), /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content-desc" }, import_utils.locales.getText("page.booking.text.unconfirmed-payments.msg")), /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-footer" }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          className: "go-payments-footer-btn",
          size: "large",
          onClick: () => {
            setModalContentState({ open: false });
          }
        },
        import_utils.locales.getText("page.booking.text.go-back")
      ), /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          className: "go-payments-footer-btn",
          type: "primary",
          size: "large",
          onClick: () => {
            goCheckout();
            setModalContentState({ open: false });
          }
        },
        import_utils.locales.getText("page.booking.text.go-to-payments")
      ))),
      footer: null
    });
  };
  const showCancelCalendarModal = () => {
    setModalContentState({
      className: "go-payments-modal-wrap",
      open: true,
      width: 480,
      onCancel: () => {
        setModalContentState({ open: false });
      },
      children: /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content-title" }, import_utils.locales.getText("page.booking.text.cancel-booking")), /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content-desc" }, import_utils.locales.getText("page.booking.text.cancel.calendar.msg")), /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-footer" }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          className: "go-payments-footer-btn",
          size: "large",
          onClick: () => {
            setModalContentState({ open: false });
          }
        },
        import_utils.locales.getText("page.booking.text.go-back")
      ), /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          className: "go-payments-footer-btn",
          type: "primary",
          size: "large",
          onClick: () => {
            setBookingStatus("cancelled");
            setModalContentState({ open: false });
          }
        },
        import_utils.locales.getText("page.booking.text.cancel-booking")
      ))),
      footer: null
    });
  };
  const showCancelledModal = () => {
    const cancelBtn = /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: "go-payments-footer-btn",
        size: "large",
        type: "primary",
        onClick: () => {
          setBookingStatus("cancelled");
          setModalContentState({ open: false });
        }
      },
      import_utils.locales.getText("page.booking.text.cancel-only")
    );
    const refundBtn = /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: "go-payments-footer-btn",
        size: "large",
        danger: true,
        ghost: true,
        onClick: () => {
          handleRefund();
          setModalContentState({ open: false });
        }
      },
      import_utils.locales.getText("page.booking.text.refund")
    );
    const okBtn = /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: "go-payments-footer-btn",
        size: "large",
        type: "primary",
        onClick: () => {
          setBookingStatus("cancelled");
          setModalContentState({ open: false });
        }
      },
      import_utils.locales.getText("page.booking.text.confirm")
    );
    const backBtn = /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: "go-payments-footer-btn",
        size: "large",
        onClick: () => {
          setModalContentState({ open: false });
        }
      },
      import_utils.locales.getText("page.booking.text.go-back")
    );
    setModalContentState({
      className: "go-payments-modal-wrap",
      open: true,
      width: 480,
      onCancel: () => {
        setModalContentState({ open: false });
      },
      children: /* @__PURE__ */ import_react.default.createElement("div", { className: "go-payments-modal-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "cancelled-modal-content-title" }, import_utils.locales.getText("page.booking.text.cancel-booking")), /* @__PURE__ */ import_react.default.createElement("div", { className: "cancelled-modal-content-desc cancelled-modal-content-desc-red" }, import_utils.locales.getText("page.booking.text.cancel-booking.msg")), isTicket ? /* @__PURE__ */ import_react.default.createElement("div", { className: "cancelled-modal-content-desc" }, import_utils.locales.getText("page.booking.text.disabled-tickets")) : null, isPaid ? /* @__PURE__ */ import_react.default.createElement("div", { className: "cancelled-modal-content-list-wrap" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "cancelled-modal-content-list-title" }, import_utils.locales.getText("page.booking.text.paid.msg")("paid"), " ", import_utils.locales.getText("page.booking.text.please-choose-an-option")), /* @__PURE__ */ import_react.default.createElement("ul", { className: "cancelled-modal-content-ul" }, /* @__PURE__ */ import_react.default.createElement("li", { className: "cancelled-modal-content-li" }, import_utils.locales.getText(
        "page.booking.text.please-choose-an-option-2"
      )), /* @__PURE__ */ import_react.default.createElement("li", { className: "cancelled-modal-content-li" }, import_utils.locales.getText(
        "page.booking.text.please-choose-an-option-1"
      )))) : null, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)("go-payments-footer", {
            "go-payments-footer-column": isPaid
          })
        },
        isPaid ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, cancelBtn, refundBtn, backBtn) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, backBtn, okBtn)
      )),
      footer: null
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "booking-header",
      style: { backgroundColor: currentStatus.color }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "booking-order-sales-channel" }, orderSalesChannel[bookingDetail == null ? void 0 : bookingDetail.order_sales_channel] || ""),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "booking-header-line-2" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "booking-header-left" }, import_utils.locales.getText("pisell2.text.status.booking_id"), ":", " ", ((_c = bookingDetail == null ? void 0 : bookingDetail.bookings) == null ? void 0 : _c.booking_id) || "-"), /* @__PURE__ */ import_react.default.createElement("div", { className: "booking-header-right" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Dropdown,
      {
        menu: { items, onClick: onSelect },
        disabled: state.appointment_status === "cancelled" || !state.bookingDetail
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "booking-header-right-status" }, currentStatus.label, state.appointment_status !== "cancelled" ? /* @__PURE__ */ import_react.default.createElement(import_icons.DownOutlined, { className: "booking-header-right-status-icon" }) : null)
    )))
  ), /* @__PURE__ */ import_react.default.createElement(import_antd.Modal, { ...modalContent }));
};
var header_default = header;
