import browser from "webextension-polyfill";
import { derived, writable } from "svelte/store";

export const instanceUrlStore = writable<string | undefined>(
  undefined,
  (set) => {
    browser.storage.local.get("instanceUrl").then((result) => {
      set(result["instanceUrl"] || "http://localhost:8093");
    });
  }
);

instanceUrlStore.subscribe((instanceUrl) => {
  browser.storage.local.set({ instanceUrl });
});

export const cleanInstanceUrl = derived(instanceUrlStore, (url) => {
  if (url) {
    return url.replace(/\/+$/g, "");
  }
});
