import { client as l, createOssInstance as d } from "@aplus-frontend/oss";
let n, p;
function F(s, e) {
  n = s, p = e;
}
const O = {
  "zh-cn": "zh_CN",
  en: "en_US"
};
function c() {
  return O[p.value] ?? "zh_CN";
}
async function U({
  file: s,
  dirName: e,
  oss: o,
  successCallBack: u,
  errorCallBack: r,
  progressCallBack: f,
  baseDirName: m = "Frontend-Upload",
  bucket: i
}) {
  if (!e) {
    r("required dirName");
    return;
  }
  try {
    const a = await o.put({
      fileName: s.name,
      dirName: e,
      data: s,
      progressCallBack: (t) => {
        f(t);
      },
      baseDirName: m
    });
    if (a.status === 204)
      return;
    if (a.status === 200) {
      const { saveUrl: t, previewUrl: w } = a, g = i && t ? `${i}:${t}` : t;
      u(g, w);
    } else
      r(a?.message || n("ap.apUpload.networkAnomaly"));
  } catch (a) {
    r(a);
  }
}
function y() {
  return { client: l, put: U };
}
async function T(s) {
  if (!s)
    throw new Error(`${n("ap.apUpload.pleasePass")}getOssAccess`);
  return await l.initOssClient({
    getOssAccess: s,
    locale: c(),
    onFailure: (e) => {
      throw new Error(
        typeof e?.message == "string" ? e.message : n("ap.apUpload.FailedToObtainOssTemporaryPermissions")
      );
    }
  }), y();
}
async function v(s) {
  if (!s)
    throw new Error(`${n("ap.apUpload.pleasePass")}getOssAccess`);
  const e = d();
  return await e.initOssClient({
    getOssAccess: s,
    locale: c(),
    onFailure: (o) => {
      throw new Error(
        typeof o?.message == "string" ? o.message : n("ap.apUpload.FailedToObtainOssTemporaryPermissions")
      );
    }
  }), e;
}
export {
  v as getOssInstance,
  F as injectLocaleToOss,
  y as useOss,
  T as useOssInit
};
