import s from "../node_modules/zustand/esm/index.es.js";
import { persist as l } from "../node_modules/zustand/esm/middleware.es.js";
import { Brand as c } from "../model/Brand/Brand.es.js";
import { NodeIconMap as p } from "../model/Brand/LibraryIconMap.es.js";
import { Frontify as n } from "../service/Frontify.es.js";
import { createTreeNode as m } from "../service/Tree/Tree.es.js";
const L = s(
  l(
    (r, d) => ({
      brands: [],
      selectedBrandId: "",
      selectedLibraryId: void 0,
      tree: [],
      selectedNode: void 0,
      setSelectedBrandId: (e) => r({
        selectedBrandId: e,
        tree: void 0,
        selectedNode: void 0,
        selectedLibraryId: void 0
      }),
      setSelectedLibraryId: (e) => {
        d().selectedLibraryId !== e && r({ selectedLibraryId: e, selectedNode: void 0, tree: void 0 });
      },
      setInitialTree: async () => {
        if (d().selectedLibraryId && d().token) {
          const e = await n.getFoldersFromNodeId(d().selectedLibraryId, d().token);
          let o = [];
          if (e) {
            const t = "browseLibrary" in e ? "browseLibrary" : "browseWorkspace";
            o = e[t].folders.items.map(
              (a) => m({ node: a, parent: e })
            ), o = [
              {
                icon: p[e.__typename],
                name: e.name,
                id: JSON.stringify({
                  id: e.id,
                  __typename: e.__typename,
                  name: e.name
                }),
                parentId: null,
                sort: 1,
                value: "value"
              },
              ...o
            ];
          }
          r({ tree: o });
        }
      },
      addToTree: async (e) => {
        var a;
        const t = [...(a = d().tree) != null ? a : [], ...e];
        r({ tree: t });
      },
      setSelectedNode: (e) => r({ selectedNode: e }),
      setBrands: async () => {
        const e = d().token;
        if (e) {
          const t = (await n.getFileHierarchy(e)).map((i) => c.filterBrandNodeByCreatePermission(i)), a = d().selectedBrandId !== "" ? d().selectedBrandId : t[0].id;
          r({
            brands: t,
            selectedBrandId: a
          });
        }
      },
      setToken: (e) => r({ token: e }),
      setTokenExpirationDate: (e) => r({ tokenExpirationDate: e })
    }),
    {
      name: "uploader-storage",
      partialize: (r) => ({
        token: r.token,
        tokenExpirationDate: r.tokenExpirationDate,
        brands: r.brands,
        selectedBrandId: r.selectedBrandId
      })
    }
  )
);
export {
  L as useUploaderStore
};
//# sourceMappingURL=uploaderStore.es.js.map
