import 'package:flutter/foundation.dart';

import 'locale_storage_backend.dart';
import 'locale_storage_stub.dart'
    if (dart.library.js_interop) 'locale_storage_web.dart';

abstract class LocaleStorage {
  static bool _forceMemory = false;
  static bool _forceSession = false;

  static void _syncTestMode() {
    if (_forceMemory) {
      localeStorage.setTestMode(LocaleStorageTestMode.memoryOnly);
      return;
    }
    if (_forceSession) {
      localeStorage.setTestMode(LocaleStorageTestMode.sessionOnly);
      return;
    }
    localeStorage.setTestMode(LocaleStorageTestMode.normal);
  }

  static String? read() => localeStorage.read();
  static void write(String locale) => localeStorage.write(locale);

  @visibleForTesting
  static void setTestModeForTests(LocaleStorageTestMode mode) {
    _forceMemory = mode == LocaleStorageTestMode.memoryOnly;
    _forceSession = mode == LocaleStorageTestMode.sessionOnly;
    _syncTestMode();
  }

  @visibleForTesting
  static void clearForTests() {
    localeStorage.clearForTests();
    _forceMemory = false;
    _forceSession = false;
  }

  @visibleForTesting
  static void seedLegacyForTests(String locale) {
    localeStorage.seedLegacyForTests(locale);
  }

  @visibleForTesting
  static LocaleStorageDebugState debugStateForTests() {
    return localeStorage.debugStateForTests();
  }

  static void forceMemoryStorageForTests(bool value) {
    _forceMemory = value;
    _syncTestMode();
  }

  static void forceSessionStorageForTests(bool value) {
    _forceSession = value;
    _syncTestMode();
  }
}
