import 'dart:convert';

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

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

  factory SubcategoryModel.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 SubcategoryModel(
      id: json["id"] ?? '',
      name: language,
      translations: (json['translations'] as List)
          .map((e) => Translation.fromJson(e))
          .toList(),
    );
  }
}

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,
      };
}
