import { getAllBrands as m, getFoldersOfLibrary as u } from "../api/query.es.js";
import { uploadFileFrontify as g, createAssetFrontify as j } from "../api/mutations.es.js";
const E = {
  getFileHierarchy: async (e) => {
    const t = await (await o(e, {
      query: m
    })).json();
    return I(t.data.brands);
  },
  getSubFoldersFromId: async (e, s) => (await (await o(s, {
    query: u(e)
  })).json()).data.node,
  getFoldersFromNodeId: async (e, s) => {
    if (e)
      return (await (await o(s, {
        query: u(e)
      })).json()).data.node;
  },
  uploadToLibrary: async (e, s, t, n, i, f, c) => {
    const l = [];
    try {
      for (const [a, r] of s.entries()) {
        const d = await (await fetch(r.url)).blob();
        i(100 / (s.length / (a / 3 + 1)));
        const y = {
          input: {
            filename: r.file,
            chunkSize: 104857600,
            size: d.size
          }
        }, p = await (await o(c, {
          query: g,
          variables: y
        })).json();
        for (const w of p.data.uploadFile.urls)
          await fetch(w, {
            method: "PUT",
            headers: {
              "content-type": "binary"
            },
            body: d
          });
        i(100 / (s.length / (a / 2 + 1)));
        const h = {
          input: {
            projectId: t,
            fileId: p.data.uploadFile.id,
            title: r.file.replace(/\.[^/.]+$/, ""),
            externalId: "",
            directory: e
          }
        }, b = {
          query: j,
          variables: h
        };
        i(100 / (s.length / (a + 1)));
        const F = await (await o(c, b)).json();
        l.push(F.data.createAsset.job.assetId);
      }
      n(l);
    } catch (a) {
      f({ status: "error", message: a || "An Error occured while fetching the asset." });
    }
  }
}, o = async (e, s) => {
  var t;
  if ((t = e == null ? void 0 : e.bearerToken) != null && t.domain)
    return await fetch(`https://${e.bearerToken.domain}/graphql`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${e.bearerToken.accessToken}`
      },
      body: JSON.stringify(s)
    });
  throw "Resource Token is not set";
}, I = (e) => e.filter((s) => s.libraries.total > 0 || s.workspaceProjects.total > 0);
export {
  E as Frontify
};
//# sourceMappingURL=Frontify.es.js.map
