// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

import '../serializers.dart';

part 'api_error.g.dart';

abstract class ApiError implements Built<ApiError, ApiErrorBuilder> {
  factory ApiError([void Function(ApiErrorBuilder)? updates]) = _$ApiError;

  ApiError._();

  @BuiltValueField(wireName: 'errors')
  BuiltList<String>? get errors;

  String toJson() {
    return json.encode(
      serializers.serializeWith(ApiError.serializer, this),
    );
  }

  static ApiError? fromJson(String jsonString) {
    return serializers.deserializeWith(
      ApiError.serializer,
      json.decode(jsonString),
    );
  }

  static Serializer<ApiError> get serializer => _$apiErrorSerializer;
}
