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/preview/FilePreview.tsx
var FilePreview_exports = {};
__export(FilePreview_exports, {
  default: () => FilePreview_default
});
module.exports = __toCommonJS(FilePreview_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_constants = require("../_util/constants");
var import_types = require("./utils/types");
var import_utils = require("./utils/index");
var import_type = require("./type");
var import_ConfigContext = __toESM(require("./context/ConfigContext"));
var import_PdfViewer = __toESM(require("./PdfViewer"));
var import_context = require("../config-provider/context");
var import_FilePreview = require("./FilePreview.less");
var TIME_LIMIT = 1e4;
var timer;
var FilePreview = (props) => {
  const componentContext = (0, import_react.useContext)(import_context.ConfigContext);
  const { uiLocale } = componentContext;
  const { getPrettyCls } = (0, import_react.useContext)(import_ConfigContext.default);
  const { allowDownload = true, className, convertStatus = import_types.ConvertStatus.PENDING, file = {}, forwardRef, locale, startPage = 1 } = props;
  const [currentStatus, setCurrentStatus] = (0, import_react.useState)(convertStatus);
  const [pageCount, setPageCount] = (0, import_react.useState)(1);
  const prettyCls = getPrettyCls(import_type.CLASS_CLS);
  const { transformId: id, name = "", url = "" } = file;
  const type = name.includes("xls") ? import_type.PreviewType.IFRAME : import_type.PreviewType.PDF;
  const classCls = prettyCls({}, "", import_type.CLASS_CLS);
  const onDownload = () => {
    (0, import_utils.download)(url, name);
  };
  const onClearTimer = () => {
    if (timer) {
      window.clearInterval(timer);
    }
  };
  const getTotalPage = (id2) => {
    import_utils.Ajax.get(
      import_constants.EHR_SERVICE + "/api/office/km/file/convert/status/" + id2,
      (res) => {
        const { totalPages, status } = res.data || {};
        if (totalPages) {
          setPageCount(totalPages);
        }
        if (`${status}` !== `${import_types.ConvertStatus.PENDING}`) {
          onClearTimer();
        }
        if (`${status}` === `${import_types.ConvertStatus.SUCCESS}`) {
          setCurrentStatus(import_types.ConvertStatus.SUCCESS);
        } else if (`${status}` === `${import_types.ConvertStatus.FAIL}`) {
          setCurrentStatus(import_types.ConvertStatus.FAIL);
        }
      },
      () => {
        console.error(uiLocale.Preview.queryError);
      }
    );
  };
  const getConvertStatus = () => {
    if (id && (currentStatus === import_types.ConvertStatus.PENDING || convertStatus === import_types.ConvertStatus.PENDING)) {
      getTotalPage(id);
    }
  };
  (0, import_ahooks.useMount)(() => {
    if (id) {
      if (currentStatus === import_types.ConvertStatus.PENDING) {
        getConvertStatus();
      } else if (currentStatus === import_types.ConvertStatus.SUCCESS && type === import_type.PreviewType.PDF) {
        getTotalPage(id);
      }
    }
    if (forwardRef == null ? void 0 : forwardRef.current) {
      forwardRef.current.onClearTimer = onClearTimer;
    }
    timer = window.setInterval(getConvertStatus, TIME_LIMIT);
  });
  (0, import_ahooks.useUnmount)(() => {
    onClearTimer();
  });
  (0, import_ahooks.useUpdateEffect)(() => {
    setPageCount(0);
    setCurrentStatus(convertStatus);
    onClearTimer();
    if (convertStatus === import_types.ConvertStatus.PENDING) {
      getConvertStatus();
      timer = window.setInterval(getConvertStatus, TIME_LIMIT);
    } else if (convertStatus === import_types.ConvertStatus.SUCCESS && type === import_type.PreviewType.PDF) {
      id && getTotalPage(id);
    }
  }, [id]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `${classCls} ${className || ""}`, ref: forwardRef }, currentStatus == import_types.ConvertStatus.PENDING && id && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_type.CLASS_CLS}-tip` }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, name, " - ", uiLocale.Preview.convertWaiting))), (currentStatus == import_types.ConvertStatus.FAIL || !id) && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_type.CLASS_CLS}-tip` }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, name, " - ", uiLocale.Preview.convertError))), currentStatus === import_types.ConvertStatus.SUCCESS && id && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_type.CLASS_CLS}-container` }, type === import_type.PreviewType.IFRAME && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_type.CLASS_CLS}-tip` }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", null, name)), /* @__PURE__ */ import_react.default.createElement("iframe", { frameBorder: "no", id: `${id}`, title: name, src: file.url })), type === import_type.PreviewType.PDF && /* @__PURE__ */ import_react.default.createElement(
    import_PdfViewer.default,
    {
      allowDownload,
      file: id,
      host: import_constants.EHR_SERVICE,
      locale,
      pageCount,
      startPage,
      title: name,
      onDownload,
      onPageChanged: (page) => console.log(`page changed: ${page}`),
      onPageLoaded: (page) => console.log(`page loaded: ${page}`),
      onToggleControls: (visible) => console.log(`controls visible: ${visible}`)
    }
  )));
};
var FilePreview_default = FilePreview;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
