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/deposit/index.tsx
var deposit_exports = {};
__export(deposit_exports, {
  default: () => deposit_default
});
module.exports = __toCommonJS(deposit_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_emptyBooking = __toESM(require("../components/emptyBooking"));
var import_tabPane = __toESM(require("../components/tabPane"));
var import_DepositItem = __toESM(require("./DepositItem"));
var import_EditDepositModal = __toESM(require("./EditDepositModal"));
var import_antd = require("antd");
var import_amount = require("../components/footer/amount");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_utils2 = require("../utils");
var import_serve = require("./serve");
var import_dayjs = __toESM(require("dayjs"));
var Deposit = (props) => {
  var _a, _b;
  const { globalState, setState } = props;
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [modal, contextHolder] = import_antd.Modal.useModal();
  const editDepositModalRef = (0, import_react.useRef)();
  const context = (0, import_useEngineContext.default)();
  const {
    isTerminal,
    interaction,
    action,
    storage,
    pisellosAllModule,
    getData
  } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const { ScheduleModule } = pisellosAllModule;
  const isTeam = (0, import_react.useMemo)(() => {
    return storage.get("channel") === "android_team_portal" || storage.get("channel") === "ios_team_portal";
  }, []);
  import_utils2.request.setRequest(context.appHelper.utils.request);
  const {
    bookingDetail,
    bookingId,
    amountSymbol,
    generatePaymentLink,
    locale,
    callback
  } = globalState;
  const sendEmail = (id) => {
    action({
      type: "pisell1.sendPayLinkEmail",
      data: {
        id,
        is_deposit: 1,
        deposit_payment_time: bookingDetail.deposit_payment_time,
        customer: bookingDetail.customer,
        contacts: bookingDetail == null ? void 0 : bookingDetail.contacts
      },
      callback: () => {
      }
    });
  };
  const create = () => {
    var _a2, _b2;
    (_b2 = (_a2 = editDepositModalRef.current) == null ? void 0 : _a2.handleOpen) == null ? void 0 : _b2.call(_a2);
  };
  const deleteDeposit = () => {
    modal.confirm({
      title: import_utils.locales.getText("pisell2.text.deposit-delete-message-title"),
      content: import_utils.locales.getText("pisell2.text.deposit-delete-message"),
      onOk() {
        (0, import_serve.editDepositAmount)(bookingDetail.id, {
          deposit_amount: 0
        }).then((res) => {
          setState({
            bookingDetail: {
              ...bookingDetail,
              deposit_amount: 0,
              is_deposit: 0
            }
          });
          callback == null ? void 0 : callback({ order_id: bookingId, afterCloseUpdate: true });
        });
      }
    });
  };
  const getDataFromHost = () => {
    try {
      const is_price_include_tax = getData("is_price_include_tax");
      const tax_rate = getData("tax_rate") || "10";
      const tax_title = getData("tax_title") || "GST";
      const surcharge_list = getData("surcharge_list");
      const schedule_list = getData("schedule_list");
      let scheduleById = {};
      if (Array.isArray(schedule_list)) {
        for (let item of schedule_list) {
          scheduleById[item.id] = item;
        }
      }
      return {
        is_price_include_tax,
        tax_rate,
        tax_title,
        surcharge_list,
        schedule_list,
        scheduleById
      };
    } catch (error) {
      console.error(error);
      return {};
    }
  };
  const total = (0, import_react.useMemo)(() => {
    return (0, import_amount.getSumTotal)(globalState, {
      isEdit: false,
      ScheduleModule,
      ...getDataFromHost()
    });
  }, [globalState]);
  const depositList = (0, import_react.useMemo)(() => {
    if (!bookingId) return [];
    if ((bookingDetail == null ? void 0 : bookingDetail.is_deposit) === 0) return [];
    return [
      {
        customer: bookingDetail == null ? void 0 : bookingDetail.customer,
        status: bookingDetail == null ? void 0 : bookingDetail.deposit_payment_status,
        totalAmount: total.expectAmount,
        depositAmount: bookingDetail == null ? void 0 : bookingDetail.deposit_amount,
        depositUnPaidAmount: bookingDetail == null ? void 0 : bookingDetail.deposit_unpaid_amount,
        depositOriginPaidAmount: bookingDetail == null ? void 0 : bookingDetail.deposit_origin_paid_amount,
        depositPaidAmount: bookingDetail == null ? void 0 : bookingDetail.deposit_paid_amount,
        createdTime: (0, import_dayjs.default)(bookingDetail == null ? void 0 : bookingDetail.created_at).format(
          locale === "en" ? "dddd, D MMMM YYYY HH:mm" : "YYYY-MM-DD HH:mm"
        ),
        orderId: bookingDetail == null ? void 0 : bookingDetail.id,
        orderNumber: bookingDetail == null ? void 0 : bookingDetail.shop_full_order_number,
        amountSymbol: (globalState == null ? void 0 : globalState.amountSymbol) || "$",
        totalAmountStr: (0, import_utils.formatAmount)(
          total.expectAmount,
          2,
          (globalState == null ? void 0 : globalState.amountSymbol) || "$"
        )
      }
    ];
  }, [
    bookingDetail,
    total == null ? void 0 : total.expectAmount,
    globalState == null ? void 0 : globalState.amountSymbol,
    bookingDetail == null ? void 0 : bookingDetail.deposit_amount
  ]);
  const isBookingCancelled = ((_b = bookingDetail == null ? void 0 : bookingDetail.bookings) == null ? void 0 : _b.appointment_status) === "cancelled";
  const isPaid = (bookingDetail == null ? void 0 : bookingDetail.deposit_payment_status) === "paid";
  const footer = (0, import_react.useMemo)(() => {
    if (!bookingId) return null;
    if (isBookingCancelled) return null;
    if (isPaid) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__booking-deposit-footer" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        size: "large",
        block: true,
        onClick: depositList.length > 0 ? deleteDeposit : create
      },
      depositList.length > 0 ? import_utils.locales.getText("pisell2.text.deposit-delete") : import_utils.locales.getText("pisell2.text.add")
    ));
  }, [depositList, isBookingCancelled, isPaid]);
  const handleEdit = (e, item) => {
    var _a2, _b2;
    (_b2 = (_a2 = editDepositModalRef.current) == null ? void 0 : _a2.handleOpen) == null ? void 0 : _b2.call(_a2, item.depositAmount);
  };
  const handleChange = (amount) => {
    (0, import_serve.editDepositAmount)(bookingDetail.id, {
      deposit_amount: amount
    }).then((res) => {
      setState({
        bookingDetail: {
          ...bookingDetail,
          deposit_amount: amount,
          is_deposit: 1
        }
      });
      callback == null ? void 0 : callback({ order_id: bookingId, afterCloseUpdate: true });
    });
  };
  const handleGoOrderDetail = () => {
    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: "edit_order",
        data: {
          order_id: bookingDetail.id
        }
      });
    }
    globalState.action({
      type: "pisell1.goOrderDetail",
      data: {
        "[unique_order_id]": bookingDetail.id
      }
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_tabPane.default,
    {
      loading,
      header: import_utils.locales.getText("pisell2.text.deposit-list"),
      footer
    },
    !bookingId || !depositList.length ? /* @__PURE__ */ import_react.default.createElement(
      import_emptyBooking.default,
      {
        description: !bookingId ? void 0 : import_utils.locales.getText("pisell2.text.deposit-list-empty")
      }
    ) : depositList.map((item) => /* @__PURE__ */ import_react.default.createElement(
      import_DepositItem.default,
      {
        key: item.status,
        ...item,
        generatePaymentLink,
        sendEmail,
        handleGoOrderDetail,
        onEdit: (e) => handleEdit(e, item),
        isPaid,
        isBookingCancelled,
        hideOrderDetail: isTeam
      }
    )),
    /* @__PURE__ */ import_react.default.createElement(
      import_EditDepositModal.default,
      {
        totalAmount: total.expectAmount,
        ref: editDepositModalRef,
        amountSymbol,
        onChange: handleChange
      }
    ),
    contextHolder
  );
};
var deposit_default = Deposit;
