'use client'
import {
  StorageClient
} from "./chunk-UOPEIK22.mjs";

// src/storage/client.ts
var M2mStorageClient = class extends StorageClient {
};
var context = {};
var getStorage = async (timeout = 5e3) => {
  const storage = await new Promise((resolve, reject) => {
    const rejectTimer = window.setTimeout(function() {
      reject(
        new Error(
          "Timeout: M2mStorageClient could not connect. Please call m2mStorageClient.init() before using m2mStorageClient."
        )
      );
    }, timeout);
    const storage2 = context.storage;
    if (storage2) {
      window.clearTimeout(rejectTimer);
      resolve(storage2);
    }
    const timer = window.setInterval(() => {
      const storage3 = context.storage;
      if (storage3) {
        window.clearTimeout(rejectTimer);
        window.clearInterval(timer);
        resolve(storage3);
      }
    }, 200);
  });
  await storage.onConnect();
  return storage;
};
var M2M_STORAGE_HUB_URL = "https://hub.m2msystems.cloud/";
var M2M_STORAGE_HUB_URL_DEV = "https://m2m-components-storage-hub-dev.netlify.app/";
var initStorageClient = (options) => {
  if (context.storage) {
    throw new Error(
      "M2mStorageClient already initialized. Do not call m2mStorageClient.init multiple times."
    );
  }
  const storage = new M2mStorageClient(
    (options == null ? void 0 : options.env) === "production" ? M2M_STORAGE_HUB_URL : M2M_STORAGE_HUB_URL_DEV
  );
  context.storage = storage;
};
var m2mStorageClient = {
  init: initStorageClient,
  getStorage
};
var M2M_AUTH_TOKEN_KEY = "M2M_APP_AUTH_TOKEN";
var getAuthTokenCache = () => {
  return window.localStorage.getItem(M2M_AUTH_TOKEN_KEY);
};
var getAuthToken = async () => {
  try {
    const storage = await getStorage();
    const autuToken = await storage.get(M2M_AUTH_TOKEN_KEY);
    return autuToken || getAuthTokenCache();
  } catch (error) {
    console.warn(error);
    return getAuthTokenCache();
  }
};
var setAuthToken = async (token) => {
  window.localStorage.setItem(M2M_AUTH_TOKEN_KEY, token);
  try {
    const storage = await getStorage();
    await storage.set(M2M_AUTH_TOKEN_KEY, token);
  } catch (error) {
    console.warn(error);
  }
};
var clearAuthToken = async () => {
  window.localStorage.removeItem(M2M_AUTH_TOKEN_KEY);
  try {
    const storage = await getStorage();
    await storage.del(M2M_AUTH_TOKEN_KEY);
  } catch (error) {
    console.warn(error);
  }
};
var m2mAuthTokenApi = {
  get: getAuthToken,
  getCache: getAuthTokenCache,
  set: setAuthToken,
  clear: clearAuthToken
};

export {
  m2mStorageClient,
  M2M_AUTH_TOKEN_KEY,
  m2mAuthTokenApi
};
//# sourceMappingURL=chunk-5FHEMJGG.mjs.map