// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:wyatt_i18n/src/core/utils/parser.dart';
import 'package:wyatt_i18n/src/core/utils/parsers/arb_parser.dart';
import 'package:wyatt_i18n/src/core/utils/parsers/json_parser.dart';
import 'package:wyatt_i18n/src/core/utils/parsers/yaml_parser.dart';

/// Enum for i18n file formats and extensions.
///
/// This enum is used to determine the parser to use for a given i18n file.
enum Format {
  /// JSON i18n file format.
  json,

  /// YAML i18n file format.
  yaml,
  yml,

  /// ARB i18n file format.
  arb;

  /// Returns the [Format] that matches the given [ext].
  static Format? fromExtension(String ext) {
    for (final format in Format.values) {
      if (format.name == ext) {
        return format;
      }
    }

    return null;
  }

  /// Returns the [Format] that matches the given [path].
  static Format? extensionOf(String path) =>
      fromExtension(path.split('.').last);

  /// Returns the [Parser] associated with this [Format].
  Parser<String, Map<String, dynamic>> get parser {
    switch (this) {
      case Format.json:
        return const JsonParser();
      case Format.yaml:
      case Format.yml:
        return const YamlParser();
      case Format.arb:
        return const ArbParser();
    }
  }
}
