init method

Future<void> init({
  1. required String appKey,
  2. required String domain,
})

Khởi tạo SDK với appKey, domain, và tự xác định platform

Implementation

Future<void> init({
  required String appKey,
  required String domain,
}) async {
  if (_initialized) {
    throw Exception('MiChat SDK has already been initialized.');
  }

  final platform = _detectPlatform();

  final response = await _authenticateWithServer(appKey, domain, platform);

  final success = response['success'] == true;
  final code = response['code'] ?? 'unknown';
  final type = response['type'] ?? 'Unknown';
  final error = response['error'] ?? 'Unknown error';

  if (!success) {
    throw Exception(
      '[$code - $type] Authentication failed: $error',
    );
  }

  _appKey = appKey;
  _domain = domain;
  _platform = platform;
  _chatUrl = response["url"].toString();
  _initialized = true;

  print('[MiChat] Init success on platform=${platform}. Url: ${chatUrl}.');
}