import { message as i } from "@aplus-frontend/antdv";
import { getOssInstance as d } from "../hook/useOss.mjs";
import { fileMatchesAccept as x } from "./accept.mjs";
let o;
function C(e) {
  o = e;
}
function m(e, r, a) {
  e.status = r, e.error = a;
}
function z(e, r, a) {
  if (a?.maxCount && a.maxCount > 1 && r.length >= a.maxCount) {
    const t = a?.maxCountErrorMsg || o("ap.apUpload.maxUploadFiles", {
      maxCount: a.maxCount
    });
    return i.warning(t), m(e, "error", t), !1;
  }
  if (a?.maxSize && (e?.size || 0) > a?.maxSize * 1024 * 1024) {
    const t = a?.maxSizeErrorMsg || o("ap.apUpload.maxUploadFileSize", {
      maxSize: a?.maxSize
    });
    return i.warning(t), m(e, "error", t), !1;
  }
  if (!x(e, a.accept)) {
    const t = a?.acceptErrorMsg || o("ap.apUpload.fileFormatNotSupported");
    return i.warning(t), m(e, "error", t), !1;
  }
  return !0;
}
const M = async (e, r) => {
  const { file: a, onError: t, onProgress: l, onSuccess: g } = e;
  try {
    const { oss: c, bucket: u } = await d({
      bucket: r?.bucket
    }), s = await c?.put({
      dirName: r?.dirName,
      fileName: a?.name,
      data: a,
      progressCallBack: (n) => {
        l?.({
          percent: n
        });
      }
    });
    if (s.status === 204)
      return;
    if (s.status === 200) {
      const { saveUrl: n } = s, f = u && n ? `${u}:${n}` : n;
      g?.({
        fileName: a?.name,
        path: f
      });
    } else
      t?.(s?.message || o("ap.apUpload.networkAnomaly"));
  } catch (c) {
    t?.(c);
  }
};
export {
  m as changeFileStatus,
  C as injectUtilUpload,
  z as uploadBeforeUpload,
  M as uploadCustomRequest
};
