import { getFileHeader } from './chunk-EYM3ANAG.js';
import { __name } from './chunk-6AXVLTU5.js';

// src/prepare/runtime/runtime/shared/id.ts
function writeId() {
  return `${getFileHeader()}

/**
 * Generate a random string
 *
 * @param array - The array to fill with random values
 * @returns The array filled with random values
 */
export function getRandom(array: Uint8Array) {
  if (array === null) {
    throw new StormError({ code: 9 });
  }

  // Fill the array with random values
  for (let i = 0; i < array.length; i++) {
    array[i] = Math.floor(Math.random() * 256); // Random byte (0-255)
  }

  return array;
}

/**
 * A platform agnostic version of the [nanoid](https://github.com/ai/nanoid) package with some modifications.
 *
 * @param size - The size of the string to generate
 * @returns A unique identifier following the nanoid format
 */
export function uniqueId(size = 24): string {
  // Use our custom getRandom function to fill a Uint8Array with random values.
  const randomBytes = getRandom(new Uint8Array(size));

  return randomBytes.reduce((id, byte) => {
    // It is incorrect to use bytes exceeding the alphabet size.
    // The following mask reduces the random byte in the 0-255 value
    // range to the 0-63 value range. Therefore, adding hacks, such
    // as empty string fallback or magic numbers, is unnecessary because
    // the bitmask trims bytes down to the alphabet size.
    byte &= 63;
    if (byte < 36) {
      // \`0-9a-z\`
      id += byte.toString(36);
    } else if (byte < 62) {
      // \`A-Z\`
      id += (byte - 26).toString(36).toUpperCase();
    } else if (byte > 62) {
      id += "-";
    } else {
      id += "_";
    }
    return id;
  }, "");
}
  `;
}
__name(writeId, "writeId");

export { writeId };
//# sourceMappingURL=chunk-ZLYR6CJC.js.map
//# sourceMappingURL=chunk-ZLYR6CJC.js.map