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 import_useRuntime = require("../../ticketBooking/hooks/useRuntime");
var translationOriginal = (text) => "";
var formatCapacityToChildrenData = (data) => {
  const map = {};
  data.forEach((item) => {
    const name = translationOriginal(item.label) || "default";
    if (!map[name]) {
      map[name] = {
        label: !item.label ? import_utils.locales.getText("pisell2.booking.dashboard.default") : item.label,
        value: 0
      };
    }
    map[name].value = (map[name].value || 0) + (item.num || 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) => {
  var _a, _b, _c;
  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 { businessUtils } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const hasTurnstileApp = !!((_b = businessUtils == null ? void 0 : businessUtils.getAppByModal) == null ? void 0 : _b.call(businessUtils, "turnstile"));
  const { isSecondaryScreen } = ((_c = import_useRuntime.useRuntime) == null ? void 0 : _c()) || { isSecondaryScreen: false };
  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 [statisticData, setStatisticData] = (0, import_react.useState)({});
  const getData = async () => {
    var _a2;
    console.log("otherParams", otherParams);
    let statisticsTime = ((_a2 = otherParams == null ? void 0 : otherParams.booking_time_start_between) == null ? void 0 : _a2[1]) || (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
    const statisticsDay = (0, import_dayjs.default)(statisticsTime);
    const now = (0, import_dayjs.default)();
    if (statisticsDay.isValid()) {
      if (statisticsDay.isSame(now, "day")) statisticsTime = now.format("YYYY-MM-DD HH:mm:ss");
      if (statisticsDay.isBefore(now, "day")) statisticsTime = statisticsDay.endOf("day").format("YYYY-MM-DD HH:mm:ss");
      if (statisticsDay.isAfter(now, "day")) statisticsTime = statisticsDay.startOf("day").format("YYYY-MM-DD HH:mm:ss");
    }
    const res = await (0, import_serve.getStatistic)({
      statistics_time: statisticsTime
    });
    setStatisticData(res);
    calcBookingDataCount();
  };
  const calcBookingDataCount = () => {
    var _a2, _b2, _c2, _d, _e, _f, _g, _h;
    let groupsOnSiteNow = statisticData.groups_today_num || 0;
    let peopleOnSiteNow = statisticData.people_on_site_now_num || (hasTurnstileApp ? 0 : "-");
    let totalVisitorsToday = ((_a2 = statisticData.total_visitors_today) == null ? void 0 : _a2.num) || 0;
    let totalVisitorsTodayChildren = ((_b2 = statisticData.total_visitors_today) == null ? void 0 : _b2.statistics) || [];
    let ongoing = ((_c2 = statisticData.ongoing_visitors) == null ? void 0 : _c2.num) || 0;
    let ongoingChildren = ((_d = statisticData.ongoing_visitors) == null ? void 0 : _d.statistics) || [];
    let upcoming = ((_e = statisticData.upcoming_visitors) == null ? void 0 : _e.num) || 0;
    let upcomingChildren = ((_f = statisticData.upcoming_visitors) == null ? void 0 : _f.statistics) || [];
    let overdue = statisticData.overdue_visitors_num || (hasTurnstileApp ? 0 : "-");
    let completed = ((_g = statisticData.completed_visitors) == null ? void 0 : _g.num) || 0;
    let completedChildren = ((_h = statisticData.completed_visitors) == null ? void 0 : _h.statistics) || [];
    let departed = statisticData.departed_visitors_num || (hasTurnstileApp ? 0 : "-");
    onDataChange == null ? void 0 : onDataChange(
      {
        groupsOnSiteNow,
        peopleOnSiteNow,
        totalVisitorsToday,
        ongoing,
        upcoming,
        overdue,
        completed,
        departed
      },
      []
    );
    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)(() => {
    if (isSecondaryScreen) return;
    return () => {
    };
  }, [isSecondaryScreen]);
  (0, import_react.useEffect)(() => {
    if (isSecondaryScreen) return;
    getData();
  }, [isSecondaryScreen, 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;
