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/Resource/index.tsx
var Resource_exports = {};
__export(Resource_exports, {
  default: () => Resource_default
});
module.exports = __toCommonJS(Resource_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_resourceDisplay = __toESM(require("../../../eventBooking/components/resourceDisplay"));
var import_materials = require("@pisell/materials");
var import_hooks = require("../../hooks");
var import_context = require("../../context");
var import_utils = require("../../utils");
var import_guests = __toESM(require("../../assets/guests.svg"));
var import_resource = __toESM(require("../../assets/resource.svg"));
var import_utils2 = require("@pisell/utils");
var import_index = require("./index.less");
var Resource = (0, import_react.forwardRef)((props, ref) => {
  const [selectedResource, setSelectedResource] = (0, import_react.useState)(0);
  const {
    appointmentBooking,
    carts,
    resourceOtherParams,
    currentStep,
    setResourceOtherParams,
    currentStepParams,
    currentSelectedResources
  } = (0, import_context.useAppointmentBookingContext)();
  const { getResourceList } = (0, import_hooks.useAppointmentBookingResource)();
  const { checkSkipResourceStep } = (0, import_hooks.useIsSkipResourceStep)();
  const { updateStep, nextStep } = (0, import_hooks.useAppointmentBookingStep)();
  const [loading, setLoading] = (0, import_react.useState)(false);
  const utils = (0, import_hooks.contextUtils)();
  (0, import_react.useImperativeHandle)(ref, () => ({
    next: async () => {
      setResourceOtherParams({
        ...resourceOtherParams,
        errorList: null
      });
      nextStep();
    }
  }));
  const resources = (0, import_react.useMemo)(() => {
    const { resources: resources2 } = getResourceList();
    return resources2 || [];
  }, []);
  (0, import_react.useEffect)(() => {
    setLoading(true);
  }, []);
  (0, import_react.useEffect)(() => {
    const checkAndSkipResourceStep = async () => {
      var _a;
      try {
        const _resources = getResourceList();
        const skipResourceStep = await checkSkipResourceStep();
        const key = (_a = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a.key;
        updateStep(key, {
          index: currentStep == null ? void 0 : currentStep.index,
          isSkip: skipResourceStep
        });
        if (skipResourceStep) {
          return true;
        }
        return false;
      } catch (error) {
      }
    };
    checkAndSkipResourceStep();
  }, [currentStepParams.key]);
  const anyResource = [
    {
      id: 0,
      image: import_guests.default,
      title: import_utils2.locales.getText("pisell2.appointment.any.professional"),
      subtitle: import_utils2.locales.getText("pisell2.appointment.for.maximum.availability")
    }
  ];
  const appointmentBoard = (0, import_react.useMemo)(() => {
    return (0, import_utils.currentAppointmentBoard)(
      currentStepParams.key,
      resources,
      appointmentBooking
    );
  }, [currentStepParams.key, resources]);
  const renderLists = (0, import_react.useMemo)(() => {
    const appointmentCarts = carts.filter(
      (n) => !(appointmentBooking == null ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id))
    );
    if (appointmentCarts.length > 1 || !(appointmentCarts == null ? void 0 : appointmentCarts.length)) {
      return [];
    }
    return (0, import_utils.currentRenderLists)(resources, appointmentBoard == null ? void 0 : appointmentBoard.form_code);
  }, [resources, appointmentBoard == null ? void 0 : appointmentBoard.form_code, carts]);
  (0, import_react.useEffect)(() => {
    const autoSelectResources = async () => {
      if (!(appointmentBoard == null ? void 0 : appointmentBoard.form_code)) {
        return;
      }
      const res = await appointmentBooking.autoSelectAllProductResources(
        appointmentBoard == null ? void 0 : appointmentBoard.form_code
      );
      setResourceOtherParams({
        ...resourceOtherParams,
        ...res
      });
      getResourceList();
      const carts2 = await appointmentBooking.getCart();
      const cartItem = carts2 == null ? void 0 : carts2[0];
      if (cartItem == null ? void 0 : cartItem._id) {
        appointmentBooking.updateCart({
          _id: cartItem == null ? void 0 : cartItem._id,
          ...cartItem
        });
      }
    };
    autoSelectResources();
  }, [appointmentBoard]);
  const resourcesResult = (0, import_react.useMemo)(() => {
    let _anyResource = (appointmentBoard == null ? void 0 : appointmentBoard.isRequired) ? [] : [...anyResource];
    return _anyResource.concat(renderLists || []);
  }, [renderLists, appointmentBoard]);
  const onClick = (e) => {
    var _a;
    setSelectedResource(e.id);
    if (e.id) {
      const index = carts.findIndex(
        (n) => !(appointmentBooking == null ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id))
      );
      e.like_status = "like";
      return appointmentBooking.updateCart({
        _id: (_a = carts == null ? void 0 : carts[index]) == null ? void 0 : _a._id,
        resources: (0, import_utils.updateCartResources)(carts == null ? void 0 : carts[index], e)
      });
    }
  };
  (0, import_react.useEffect)(() => {
    var _a, _b, _c;
    const index = carts.findIndex(
      (n) => !(appointmentBooking == null ? void 0 : appointmentBooking.isTargetCartIdNormalProduct(n._id))
    );
    const selectedResource2 = (_c = (_b = (_a = carts == null ? void 0 : carts[index]) == null ? void 0 : _a._origin) == null ? void 0 : _b.resources) == null ? void 0 : _c.find(
      (item) => (item == null ? void 0 : item.form_id) === (appointmentBoard == null ? void 0 : appointmentBoard.form_id)
    );
    setSelectedResource((selectedResource2 == null ? void 0 : selectedResource2.id) || 0);
  }, [carts, appointmentBoard == null ? void 0 : appointmentBoard.form_id]);
  (0, import_react.useEffect)(() => {
    if (resourcesResult) {
      setLoading(false);
    }
  }, [resourcesResult]);
  const currentResourceName = (0, import_react.useMemo)(() => {
    var _a;
    const currentResource = (_a = currentSelectedResources == null ? void 0 : currentSelectedResources[0]) == null ? void 0 : _a.currentSelectedResource;
    const resourceItem = (currentResource == null ? void 0 : currentResource.resourceItem) || {};
    const name = (0, import_utils.getResourceTitle)(
      appointmentBooking,
      resourceItem == null ? void 0 : resourceItem.code,
      utils,
      resourceItem
    );
    return name;
  }, [currentSelectedResources]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-resource-wrap" }, loading ? /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { loading: true, active: true }) : !(resourcesResult == null ? void 0 : resourcesResult.length) ? /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellEmpty,
    {
      description: import_utils2.locales.getText("pisell2.appointment.resource.tip")(
        currentResourceName
      ),
      maxWidth: 400,
      icon: /* @__PURE__ */ import_react.default.createElement(import_materials.Image, { src: import_resource.default, preview: false, width: 44 })
    }
  ) : /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellCustomCheckboxGroup,
    {
      dataSource: resourcesResult,
      renderItem: (item) => {
        var _a;
        return /* @__PURE__ */ import_react.default.createElement(
          import_resourceDisplay.default,
          {
            key: item.id,
            mode: "rule",
            dataSource: item,
            direction: "vertical",
            children: !(item == null ? void 0 : item.image) ? (_a = item == null ? void 0 : item.title) == null ? void 0 : _a.charAt(0).toUpperCase() : null,
            selected: selectedResource === (item == null ? void 0 : item.id),
            className: !item.id ? "appointment-resource-guest-card" : ""
          }
        );
      },
      columns: (0, import_utils2.isMobile)() ? 2 : 3,
      horizontalGap: 16,
      verticalGap: 16,
      onClick: (e) => onClick && onClick(e)
    }
  ));
});
var Resource_default = Resource;
