import { Base44DatabaseAdapter } from '@/adapters/Base44DatabaseAdapter';
import { Base44AuthAdapter } from '@/adapters/Base44AuthAdapter';
import { Base44FileStorageAdapter } from '@/adapters/Base44FileStorageAdapter';
import { Base44FunctionAdapter } from '@/adapters/Base44FunctionAdapter';

export const adapters = { database: Base44DatabaseAdapter, auth: Base44AuthAdapter, files: Base44FileStorageAdapter, functions: Base44FunctionAdapter };
const secureUuidV4 = () => {
  const cryptoApi = globalThis.crypto;

  if (
    cryptoApi
    && typeof cryptoApi.randomUUID === 'function'
  ) {
    return cryptoApi.randomUUID();
  }

  if (
    !cryptoApi
    || typeof cryptoApi.getRandomValues !== 'function'
  ) {
    throw new Error(
      'Secure identifier generation is unavailable in this browser.'
    );
  }

  const bytes = new Uint8Array(16);
  cryptoApi.getRandomValues(bytes);

  bytes[6] = (bytes[6] & 0x0f) | 0x40;
  bytes[8] = (bytes[8] & 0x3f) | 0x80;

  const hex = Array.from(
    bytes,
    value => value.toString(16).padStart(2, '0')
  ).join('');

  return [
    hex.slice(0, 8),
    hex.slice(8, 12),
    hex.slice(12, 16),
    hex.slice(16, 20),
    hex.slice(20),
  ].join('-');
};

export const newUuid = prefix =>
  `${prefix}-${secureUuidV4()}`;
export const migrationFields = (actor = 'Workshop Owner') => ({ created_by_name: actor, updated_by_name: actor, revision: 1, record_state: 'active', migration_version: 1 });