const { randomUUID } = require("crypto");

let store;

const storeOptions = {
  defaults: {
    deviceId: randomUUID(),
    showChangeLog: true,
    enableNotifications: true,
    filePaths: [],
    accepted_ins_co: [],
    runWatcherOnStartup: true,
    polling: {
      enabled: false,
      pollingInterval: 30000
    },
    ins_rule_set: null
  }
};

async function initializeStore() {
  if (!store) {
    const { default: Store } = await import("electron-store");
    store = new Store(storeOptions);
  }

  return store;
}

function getStore() {
  if (!store) {
    throw new Error("Electron store was accessed before initialization.");
  }

  return store;
}

const storeProxy = new Proxy(
  {},
  {
    get(_target, prop) {
      const value = getStore()[prop];
      return typeof value === "function" ? value.bind(getStore()) : value;
    }
  }
);

exports.initializeStore = initializeStore;
exports.store = storeProxy;
