import 'dart:convert';

class CategoryModel {
  CategoryModel({
    required this.id,
    required this.name,
    required this.translations,
    required this.icon,
  });

  final int id;
  final String name;
  final List<Translation> translations;
  final Iconca icon;

  factory CategoryModel.fromJson(
      Map<String, dynamic> json, String currentLang) {
    final List<Translation> translations = (json['translations'] as List)
        .map((e) => Translation.fromJson(e))
        .toList();

    String language = json['name'] ?? '';
    for (Translation item in translations) {
      if (item.locale == currentLang) language = item.attributeData['name'];
    }

    return CategoryModel(
      id: json["id"] ?? '',
      name: language,
      translations: (json['translations'] as List)
          .map((e) => Translation.fromJson(e))
          .toList(),
      icon: Iconca.fromJson(json["icon"] ?? ""),
    );
  }
}

class Iconca {
  Iconca({
    required this.id,
    required this.path,
  });

  final int id;
  final String path;

  factory Iconca.fromJson(Map<String, dynamic> json) => Iconca(
        id: json["id"] ?? 0,
        path: json["path"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "path": path,
      };
}

class Translation {
  Translation({
    required this.locale,
    required this.modelId,
    required this.attributeData,
  });

  final String locale;
  final String modelId;
  final Map attributeData;

  factory Translation.fromJson(Map<String, dynamic> jsn) => Translation(
        locale: jsn["locale"],
        modelId: jsn["model_id"],
        attributeData: json.decode(jsn["attribute_data"]),
      );

  Map<String, dynamic> toJson() => {
        "locale": locale,
        "model_id": modelId,
        "attribute_data": attributeData,
      };
}
