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/appointmentBooking/components/Form/index.tsx
var Form_exports = {};
__export(Form_exports, {
  default: () => Form_default
});
module.exports = __toCommonJS(Form_exports);
var import_react = __toESM(require("react"));
var import_useComponents = __toESM(require("../../../../hooks/useComponents"));
var import_utils = require("../../utils");
var import_context = require("../../context");
var import_hooks = require("../../hooks");
var import_utils2 = require("./utils");
var import_utils3 = require("../../utils");
var import_index = require("./index.less");
var Form = (0, import_react.forwardRef)((props, ref) => {
  const manualRef = (0, import_react.useRef)();
  const { appointmentBooking, setLoading, currentStepParams, carts, cartDrawerOpen, setCartDrawerOpen } = (0, import_context.useAppointmentBookingContext)();
  const { getResourceList } = (0, import_hooks.useAppointmentBookingResource)();
  const { setOtherData, getOtherData, isFormSubject } = (0, import_hooks.useAppointmentBookingSetting)();
  const { nextStep } = (0, import_hooks.useAppointmentBookingStep)();
  const { isLogin } = (0, import_hooks.useAppointmentBookingLogin)();
  const { checkAccount } = (0, import_hooks.useAppointmentBookingAccount)();
  const utils = (0, import_hooks.contextUtils)();
  const initLog = async (stepLists) => {
    var _a;
    const dates = await appointmentBooking.getDateRange();
    (0, import_utils3.nextStepLog)(utils, "初始点击继续按钮", [
      { key: "选择的日期", value: ((_a = dates == null ? void 0 : dates[0]) == null ? void 0 : _a.date) || "" },
      { key: "步骤列表", value: JSON.stringify(stepLists) },
      { key: "购物车数据", value: JSON.stringify(carts) }
    ]);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    next: () => {
      var _a, _b, _c;
      const stepLists = (currentStepParams == null ? void 0 : currentStepParams.stepLists) || [];
      const nextStepKey = (_a = stepLists[(currentStepParams == null ? void 0 : currentStepParams.index) + 1]) == null ? void 0 : _a.key;
      initLog(stepLists);
      setLoading(true);
      (_c = (_b = manualRef.current) == null ? void 0 : _b.submit) == null ? void 0 : _c.call(_b);
    }
  }));
  const [FormCard] = (0, import_useComponents.default)({
    components: ["FormCard"]
  });
  const key = "booking_form";
  const formStorage = getOtherData("booking_form") || {};
  const appointmentBoard = (0, import_react.useMemo)(() => {
    const { resources } = getResourceList();
    return (0, import_utils.currentAppointmentBoard)(
      currentStepParams.key,
      resources,
      appointmentBooking
    );
  }, [currentStepParams.key]);
  const formId = (0, import_react.useMemo)(() => {
    return (appointmentBoard == null ? void 0 : appointmentBoard.resource_form_id) || 0;
  }, [appointmentBoard == null ? void 0 : appointmentBoard.resource_form_id]);
  const updateFormStatus = (status) => {
    if (status === "error") {
      setLoading(false);
      (0, import_utils3.nextStepLog)(utils, "表单内部错误, 请填写完整表单", [
        { key: "isMobile", value: JSON.stringify(utils == null ? void 0 : utils.isMobile()) },
        { key: "cartDrawerOpen", value: JSON.stringify(cartDrawerOpen) }
      ]);
      console.log("isErrorHolder11", utils, utils == null ? void 0 : utils.isMobile(), cartDrawerOpen);
      if (cartDrawerOpen) {
        setCartDrawerOpen(false);
      }
    }
  };
  const setFormStorage = (values) => {
    const oldData = getOtherData(key);
    const newData = (0, import_utils.updateBookingFormStorage)(
      oldData,
      currentStepParams == null ? void 0 : currentStepParams.key,
      values
    );
    setOtherData(key, { ...newData });
    appointmentBooking.updateCart({
      _id: appointmentBoard == null ? void 0 : appointmentBoard.cart_id,
      relationForms: (0, import_utils2.updateRelationForms)(
        carts,
        appointmentBoard,
        values,
        currentStepParams == null ? void 0 : currentStepParams.key
      )
    });
  };
  const handleFormFinish = (status, values) => {
    try {
      updateFormStatus(status);
      if (status === "success") {
        (0, import_utils3.nextStepLog)(utils, "表单提交成功执行下一步", [
          { key: "表单值", value: JSON.stringify(values) }
        ]);
        setFormStorage(values);
        nextStep(true);
      }
    } catch (error) {
      setLoading(false);
      const msg = (error == null ? void 0 : error.message) || "未知错误";
      (0, import_utils3.nextStepLog)(utils, "表单提交失败", [
        { key: "错误信息", value: JSON.stringify(msg) }
      ]);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-form-wrap", key: currentStepParams == null ? void 0 : currentStepParams.key }, /* @__PURE__ */ import_react.default.createElement(
    FormCard,
    {
      id: formId,
      ref: manualRef,
      isGetLastData: false,
      setting: { isHidden: 1 },
      formWrapStyle: { padding: "16px" },
      isV2API: true,
      isShowSkeleton: true,
      onSubmit: (status, values) => {
        handleFormFinish(status, values);
      },
      onFinishFailed: (e) => {
        updateFormStatus("error");
      },
      onFormValueChange: (values) => {
      },
      defaultValues: (formStorage == null ? void 0 : formStorage[currentStepParams == null ? void 0 : currentStepParams.key]) || {},
      isClearData: false,
      onCancelLoading: () => {
        setLoading(false);
      }
    }
  )));
});
var Form_default = Form;
