import { createClient } from "@vercel/kv";
import { defineDriver, normalizeKey, joinKeys, createError } from "./utils/index.mjs";
export default defineDriver((opts) => {
  const base = normalizeKey(opts?.base);
  const r = (...keys) => joinKeys(base, ...keys);
  let _client;
  const getClient = () => {
    if (!_client) {
      const envPrefix = typeof process !== "undefined" && opts.env !== false ? `${opts.env || "KV"}_` : "";
      if (!opts.url) {
        const envName = envPrefix + "REST_API_URL";
        if (envPrefix && process.env[envName]) {
          opts.url = process.env[envName];
        } else {
          throw createError(
            "vercel-kv",
            `missing required \`url\` option or '${envName}' env.`
          );
        }
      }
      if (!opts.token) {
        const envName = envPrefix + "REST_API_TOKEN";
        if (envPrefix && process.env[envName]) {
          opts.token = process.env[envName];
        } else {
          throw createError(
            "vercel-kv",
            `missing required \`token\` option or '${envName}' env.`
          );
        }
      }
      _client = createClient(opts);
    }
    return _client;
  };
  return {
    hasItem(key) {
      return getClient().exists(r(key)).then(Boolean);
    },
    getItem(key) {
      return getClient().get(r(key));
    },
    setItem(key, value) {
      return getClient().set(r(key), value).then(() => {
      });
    },
    removeItem(key) {
      return getClient().del(r(key)).then(() => {
      });
    },
    getKeys(base2) {
      return getClient().keys(r(base2, "*"));
    },
    async clear(base2) {
      const keys = await getClient().keys(r(base2, "*"));
      if (keys.length === 0) {
        return;
      }
      return getClient().del(...keys).then(() => {
      });
    }
  };
});
