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/dashboard/index.tsx
var dashboard_exports = {};
__export(dashboard_exports, {
  default: () => dashboard_default
});
module.exports = __toCommonJS(dashboard_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_dayjs = __toESM(require("dayjs"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_utils2 = require("../utils");
var import_serve = require("./serve");
var import_locales = __toESM(require("../locales"));
var import_locales2 = require("../../../utils/locales");
var translationOriginal = (text) => "";
var formatCapacityToChildrenData = (data) => {
  const map = {};
  data.forEach((item) => {
    const name = translationOriginal(item.name) || "default";
    if (!map[name]) {
      map[name] = {
        label: !item.id ? import_utils.locales.getText("pisell2.booking.dashboard.default") : translationOriginal(item.name) || "",
        value: 0
      };
    }
    map[name].value = (map[name].value || 0) + (item.value || 0);
  });
  return Object.values(map).filter((item) => !!item.value).sort((a, b) => b.label.localeCompare(a.label));
};
import_utils.locales.init(import_locales.default, (0, import_locales2.getLocale)());
var BookingDashboard = (0, import_react.forwardRef)((props, ref) => {
  const {
    otherParams = {},
    backgroundColors,
    labelColor,
    valueColor,
    refreshKey,
    onDataChange,
    ...others
  } = props;
  const context = (0, import_useEngineContext.default)();
  import_utils2.request.setRequest(context.appHelper.utils.request);
  translationOriginal = context.appHelper.utils.translationOriginal;
  const [data, setData] = (0, import_react.useState)([
    // 当前在场团体
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.groups-on-site-now"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.groups-on-site-now-tip")
    },
    // 当前在场人数
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.people-on-site-now"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.people-on-site-now-tip")
    },
    // 今日游客总数
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.total-visitors-today"),
      value: "-",
      tip: import_utils.locales.getText(
        "pisell2.booking.dashboard.total-visitors-today-tip"
      )
    },
    // 正在进行中的游客
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.ongoing-visitors"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.ongoing-visitors-tip")
    },
    // 即将到达的游客
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.upcoming-visitors"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.upcoming-visitors-tip")
    },
    // 超时的游客
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.overdue-visitors"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.overdue-visitors-tip")
    },
    // 已完成的游客
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.completed-visitors"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.completed-visitors-tip")
    },
    // 已离场的游客
    {
      label: import_utils.locales.getText("pisell2.booking.dashboard.departed-visitors"),
      value: "-",
      tip: import_utils.locales.getText("pisell2.booking.dashboard.departed-visitors-tip")
    }
  ]);
  const [bookingData, setBookingData] = (0, import_react.useState)([]);
  const getData = async () => {
    const data2 = await (0, import_serve.getBookingList)({
      ...otherParams,
      item_type: "appointment_booking",
      parent_id: 0,
      with: ["children"],
      child_booking: 0,
      skip: 1,
      num: 1e3
    });
    setBookingData(data2.list);
  };
  const calcBookingDataCount = (data2) => {
    let groupsOnSiteNow = "-";
    let peopleOnSiteNow = "-";
    let totalVisitorsToday = 0;
    let totalVisitorsTodayChildren = [];
    let ongoing = 0;
    let ongoingChildren = [];
    let upcoming = 0;
    let upcomingChildren = [];
    let overdue = "-";
    let completed = 0;
    let completedChildren = [];
    let departed = "-";
    data2.forEach((item) => {
      if ((0, import_utils.isArr)(item.children)) {
        item.children.forEach((child) => {
          var _a, _b, _c, _d;
          const start = (0, import_dayjs.default)(`${child.start_date} ${child.start_time}`);
          const end = (0, import_dayjs.default)(`${child.end_date} ${child.end_time}`);
          totalVisitorsToday += child.number;
          totalVisitorsTodayChildren.push(...((_a = child.metadata) == null ? void 0 : _a.capacity) || []);
          if (start < (0, import_dayjs.default)() && end > (0, import_dayjs.default)()) {
            ongoing += child.number || 1;
            ongoingChildren.push(...((_b = child.metadata) == null ? void 0 : _b.capacity) || []);
          }
          if ((0, import_dayjs.default)() < start) {
            upcoming += child.number || 1;
            upcomingChildren.push(...((_c = child.metadata) == null ? void 0 : _c.capacity) || []);
          }
          if ((0, import_dayjs.default)() > end) {
            completed += child.number || 1;
            completedChildren.push(...((_d = child.metadata) == null ? void 0 : _d.capacity) || []);
          }
        });
      }
    });
    onDataChange == null ? void 0 : onDataChange(
      {
        groupsOnSiteNow,
        peopleOnSiteNow,
        totalVisitorsToday,
        ongoing,
        upcoming,
        overdue,
        completed,
        departed
      },
      data2
    );
    setData([
      // 当前在场团体
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.groups-on-site-now"),
        tip: import_utils.locales.getText(
          "pisell2.booking.dashboard.groups-on-site-now-tip"
        ),
        value: `${groupsOnSiteNow}`
      },
      // 当前在场人数
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.people-on-site-now"),
        tip: import_utils.locales.getText(
          "pisell2.booking.dashboard.people-on-site-now-tip"
        ),
        value: `${peopleOnSiteNow}`
      },
      // 今日游客总数
      {
        label: import_utils.locales.getText(
          "pisell2.booking.dashboard.total-visitors-today"
        ),
        tip: import_utils.locales.getText(
          "pisell2.booking.dashboard.total-visitors-today-tip"
        ),
        value: `${totalVisitorsToday}`,
        children: formatCapacityToChildrenData(totalVisitorsTodayChildren)
      },
      // 正在进行中的游客
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.ongoing-visitors"),
        tip: import_utils.locales.getText("pisell2.booking.dashboard.ongoing-visitors-tip"),
        value: `${ongoing}`,
        children: formatCapacityToChildrenData(ongoingChildren)
      },
      // 即将到达的游客
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.upcoming-visitors"),
        tip: import_utils.locales.getText("pisell2.booking.dashboard.upcoming-visitors-tip"),
        value: `${upcoming}`,
        children: formatCapacityToChildrenData(upcomingChildren)
      },
      // 超时的游客
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.overdue-visitors"),
        tip: import_utils.locales.getText("pisell2.booking.dashboard.overdue-visitors-tip"),
        value: `${overdue}`
      },
      // 已完成的游客
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.completed-visitors"),
        tip: import_utils.locales.getText(
          "pisell2.booking.dashboard.completed-visitors-tip"
        ),
        value: `${completed}`,
        children: formatCapacityToChildrenData(completedChildren)
      },
      // 已离场的游客
      {
        label: import_utils.locales.getText("pisell2.booking.dashboard.departed-visitors"),
        tip: import_utils.locales.getText("pisell2.booking.dashboard.departed-visitors-tip"),
        value: `${departed}`
      }
    ]);
  };
  (0, import_react.useEffect)(() => {
    calcBookingDataCount(bookingData);
    const timer = setInterval(() => {
      calcBookingDataCount(bookingData);
    }, 60 * 1e3);
    return () => {
      clearInterval(timer);
    };
  }, [bookingData]);
  (0, import_react.useEffect)(() => {
    getData();
    const timer = setInterval(() => {
      getData();
    }, 10 * 60 * 1e3);
    return () => {
      clearInterval(timer);
    };
  }, [JSON.stringify(otherParams), refreshKey]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    refresh: () => {
      getData();
    }
  }));
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellStatisticList,
    {
      ...others,
      data,
      backgroundColors,
      labelColor,
      valueColor
    }
  );
});
var dashboard_default = BookingDashboard;
