import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;

class ErrorCopy {
  ErrorCopy._(this._table);
  final Map<String, String> _table;

  static ErrorCopy? _instance;

  static Future<ErrorCopy> load() async {
    if (_instance != null) return _instance!;
    final raw = await rootBundle.loadString('assets/error-copy.json');
    final m = (jsonDecode(raw) as Map).cast<String, dynamic>().map(
          (k, v) => MapEntry(k, v.toString()),
        );
    _instance = ErrorCopy._(m);
    return _instance!;
  }

  String forCode(String code) =>
      _table[code] ?? _table['unknown'] ?? 'Something went wrong.';
}
