globalThis.process ??= {}; globalThis.process.env ??= {};
import require$$0 from '.prisma/client/default';

/* @ts-self-types="./index.d.ts" */
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes));
let customRandom = (alphabet, defaultSize, getRandom) => {
  let mask = (2 << Math.log2(alphabet.length - 1)) - 1;
  let step = -~((1.6 * mask * defaultSize) / alphabet.length);
  return (size = defaultSize) => {
    let id = '';
    while (true) {
      let bytes = getRandom(step);
      let j = step | 0;
      while (j--) {
        id += alphabet[bytes[j] & mask] || '';
        if (id.length >= size) return id
      }
    }
  }
};
let customAlphabet = (alphabet, size = 21) =>
  customRandom(alphabet, size | 0, random);

var _default;
var hasRequired_default;

function require_default () {
	if (hasRequired_default) return _default;
	hasRequired_default = 1;
	_default = {
	  ...require$$0,
	};
	return _default;
}

var _defaultExports = /*@__PURE__*/ require_default();

const globalForPrisma = globalThis;
const prisma = globalForPrisma.prisma ?? new _defaultExports.PrismaClient();

const nanoid = customAlphabet("23456789abcdefghijkmnpqrstuvwxyz", 6);
async function generateShortLink(fullUrl) {
  let shortCode = "";
  let exists = true;
  while (exists) {
    shortCode = nanoid();
    const existing = await prisma.shortLink.findUnique({
      where: { shortCode }
    });
    exists = !!existing;
  }
  await prisma.shortLink.create({
    data: {
      shortCode,
      fullUrl
    }
  });
  const domain = process.env.DOMAIN || "localhost:4321";
  return `${domain}/${shortCode}`;
}
async function resolveShortLink(shortCode) {
  const link = await prisma.shortLink.findUnique({
    where: { shortCode }
  });
  return link?.fullUrl || null;
}

export { generateShortLink as g, resolveShortLink as r };
