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/TimeSlicing/index.tsx
var TimeSlicing_exports = {};
__export(TimeSlicing_exports, {
  default: () => TimeSlicing_default
});
module.exports = __toCommonJS(TimeSlicing_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_context = require("../../context");
var import_hooks = require("../../hooks");
var import_utils2 = require("../../utils");
var import_index = require("./index.less");
var TimeSlicing = (0, import_react.forwardRef)((props, ref) => {
  const [selectedResource, setSelectedResource] = (0, import_react.useState)("");
  const {
    times,
    appointmentBooking,
    setSelectedTime,
    resourceOtherParams,
    carts
  } = (0, import_context.useAppointmentBookingContext)();
  const { getResourceTimeSlot, getResourceList } = (0, import_hooks.useAppointmentBookingResource)();
  const { nextStep } = (0, import_hooks.useAppointmentBookingStep)();
  (0, import_react.useImperativeHandle)(ref, () => ({
    next: () => {
      const isUnSelectedTimes = appointmentBooking.checkCartItems("date");
      if ((isUnSelectedTimes == null ? void 0 : isUnSelectedTimes.length) > 0) {
        (0, import_utils2.Toast)(import_utils.locales.getText("pisell2.appointment.unselect.time"));
        return;
      }
      nextStep();
    }
  }));
  (0, import_react.useEffect)(() => {
    getResourceTimeSlot();
  }, []);
  const getAutoSelect = async (val) => {
    setSelectedTime(val);
    const result = await appointmentBooking.submitTimeSlot(val);
  };
  (0, import_react.useEffect)(() => {
    var _a, _b, _c;
    let appoimentBookingProductCart = carts.filter((item) => !appointmentBooking.isTargetCartIdNormalProduct(item == null ? void 0 : item._id));
    if ((times == null ? void 0 : times.length) > 0 && (appoimentBookingProductCart == null ? void 0 : appoimentBookingProductCart.length) > 0) {
      const startTime = ((_a = appoimentBookingProductCart == null ? void 0 : appoimentBookingProductCart[0]) == null ? void 0 : _a.start_time) || ((_b = times == null ? void 0 : times[0]) == null ? void 0 : _b.start_time);
      setSelectedResource(startTime);
      if ((_c = appoimentBookingProductCart == null ? void 0 : appoimentBookingProductCart[0]) == null ? void 0 : _c.start_time) return;
      getAutoSelect(times == null ? void 0 : times[0]);
    }
  }, [times, carts]);
  const onClick = async (e) => {
    setSelectedResource(e == null ? void 0 : e.start_time);
    getAutoSelect(e);
  };
  return (times == null ? void 0 : times.length) > 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-time-slicing-wrap" }, times.map((item, index) => {
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: index,
        onClick: () => onClick(item),
        className: `item ${selectedResource === (item == null ? void 0 : item.start_time) ? "active" : ""}`
      },
      /* @__PURE__ */ import_react.default.createElement("span", null, item == null ? void 0 : item.start_time),
      /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-chevron-right", className: `icon` })
    );
  })) : /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellEmpty,
    {
      description: import_utils.locales.getText("pisell2.appointment.unselect.time")
    }
  );
});
var TimeSlicing_default = TimeSlicing;
