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/plus/pisellSalesManagement/components/BookingFormsPreview.tsx
var BookingFormsPreview_exports = {};
__export(BookingFormsPreview_exports, {
  default: () => BookingFormsPreview_default
});
module.exports = __toCommonJS(BookingFormsPreview_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_forms = __toESM(require("../../../components/booking/forms/forms"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_serve = require("../serve");
var BookingFormsPreview = (props) => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const utils = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
  const {
    value,
    uniqueTables,
    bookingLabels,
    textStyles,
    relationId,
    relationType,
    customer
  } = props;
  const appointmentForms = Array.isArray(value) ? value : [];
  const [modalOpen, setModalOpen] = (0, import_react.useState)(false);
  const [activeTable, setActiveTable] = (0, import_react.useState)(null);
  const _getRelationRecords = (0, import_ahooks.useRequest)(import_serve.getRelationRecords, {
    manual: true
  });
  const [hasRequested, setHasRequested] = (0, import_react.useState)(false);
  const [tooltipOpen, setTooltipOpen] = (0, import_react.useState)(false);
  const countsByFormId = (0, import_react.useMemo)(() => {
    return appointmentForms.reduce(
      (result, item) => {
        const formId = item == null ? void 0 : item.form_id;
        if (formId != null) {
          const key = String(formId);
          result[key] = (result[key] || 0) + 1;
        }
        return result;
      },
      {}
    );
  }, [appointmentForms]);
  const tableItems = (0, import_react.useMemo)(() => {
    return (uniqueTables || []).map((t) => {
      var _a2, _b, _c, _d;
      const baseTitle = ((_c = (_b = (_a2 = t == null ? void 0 : t.form) == null ? void 0 : _a2.setting) == null ? void 0 : _b.title) == null ? void 0 : _c.original) || ((_d = t == null ? void 0 : t.form) == null ? void 0 : _d.title) || "";
      if (!baseTitle) return null;
      const formId = t == null ? void 0 : t.form_id;
      const count = formId != null ? countsByFormId[String(formId)] || 0 : 0;
      const title = `${baseTitle} (${count})`;
      return {
        title,
        baseTitle,
        formId,
        count,
        table: t
      };
    }).filter((t) => t);
  }, [uniqueTables, countsByFormId]);
  const showAll = tableItems.length <= 3;
  const displayItems = showAll ? tableItems : tableItems.slice(0, 3);
  const moreCount = tableItems.length - displayItems.length;
  const handleOpenModal = (0, import_react.useCallback)(
    (item, event) => {
      event.stopPropagation();
      setActiveTable(item);
      setModalOpen(true);
      setTooltipOpen(false);
      if (!_getRelationRecords.loading && !hasRequested) {
        setHasRequested(true);
        _getRelationRecords.run({
          relation_type: relationType || "appointment_booking",
          relation_id: relationId
        });
      }
    },
    [_getRelationRecords, relationId, hasRequested, relationType]
  );
  const relationRecords = (0, import_react.useMemo)(() => {
    return Array.isArray(_getRelationRecords.data) ? _getRelationRecords.data : [];
  }, [_getRelationRecords.data]);
  const selectedForm = (0, import_react.useMemo)(() => {
    if (!(activeTable == null ? void 0 : activeTable.formId)) return null;
    const matchedRecord = relationRecords.find(
      (item) => String(item == null ? void 0 : item.form_id) === String(activeTable == null ? void 0 : activeTable.formId)
    );
    return matchedRecord;
  }, [activeTable, relationRecords]);
  if (tableItems.length === 0) {
    return /* @__PURE__ */ import_react.default.createElement("div", { style: { minWidth: 0, maxWidth: "100%", textAlign: "center" } }, "-");
  }
  const tooltipContent = /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        display: "flex",
        flexDirection: "column",
        gap: 10
      }
    },
    tableItems.map((item, idx) => {
      const clickable = item.count > 0;
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: `${item.title}-${idx}`,
          style: {
            fontSize: 12,
            color: clickable ? "#7f56d9" : "#98A2B3",
            textDecoration: clickable ? "underline" : "none",
            whiteSpace: "nowrap",
            lineHeight: "18px",
            cursor: clickable ? "pointer" : "default"
          },
          onClick: (event) => {
            if (!clickable) return;
            handleOpenModal(item, event);
          }
        },
        item.title
      );
    })
  );
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      onClick: (event) => event.stopPropagation(),
      style: {
        minWidth: 0,
        maxWidth: "100%",
        overflow: "hidden",
        boxSizing: "border-box"
      }
    },
    showAll ? /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", flexDirection: "column" } }, displayItems.map((item, idx) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: `${item.title}-${idx}`,
        style: {
          ...(textStyles == null ? void 0 : textStyles.text) || {},
          color: "#7f56d9",
          textDecoration: "underline",
          cursor: "pointer",
          textAlign: "left",
          overflow: "hidden",
          textOverflow: "ellipsis",
          whiteSpace: "nowrap"
        },
        onClick: (event) => handleOpenModal(item, event)
      },
      item.title
    ))) : /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", flexDirection: "column" } }, displayItems.map((item, idx) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: `${item.title}-${idx}`,
        style: {
          ...(textStyles == null ? void 0 : textStyles.text) || {},
          color: "#475467",
          textDecoration: "none",
          cursor: "default",
          textAlign: "left",
          overflow: "hidden",
          textOverflow: "ellipsis",
          whiteSpace: "nowrap"
        }
      },
      item.title
    )), moreCount > 0 ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Tooltip,
      {
        title: tooltipContent,
        open: modalOpen ? false : tooltipOpen,
        onOpenChange: (nextOpen) => {
          if (!modalOpen) {
            setTooltipOpen(nextOpen);
          }
        }
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            ...(textStyles == null ? void 0 : textStyles.text) || {},
            color: "#7f56d9",
            textDecoration: "underline",
            cursor: "default",
            alignSelf: "flex-start"
          }
        },
        `+${moreCount} ${import_utils.locales.getText("pisell2.salesManagement.booking.common.more")}`
      )
    ) : null),
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        open: modalOpen,
        title: bookingLabels == null ? void 0 : bookingLabels.forms,
        width: 600,
        footer: null,
        destroyOnClose: true,
        onCancel: () => setModalOpen(false)
      },
      _getRelationRecords.loading || !hasRequested ? /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            padding: "48px 0"
          }
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, null)
      ) : selectedForm ? /* @__PURE__ */ import_react.default.createElement(
        import_forms.default,
        {
          forms: [selectedForm],
          value: { [selectedForm.form_id]: selectedForm.form_record || [] },
          onChange: () => {
          },
          addCallback: () => {
          },
          client: customer,
          action: utils.action
        }
      ) : null
    )
  );
};
var BookingFormsPreview_default = BookingFormsPreview;
