// ignore_for_file: public_member_api_docs, sort_constructors_first
// 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:brick_generator/models/boolean_name_single_mapping.dart';
import 'package:yaml/yaml.dart';

const _foldersKey = 'folders';
const _filesKey = 'files';

class BooleanMapping {
  BooleanMapping({
    required this.booleanName,
    required this.folders,
    required this.files,
  });

  factory BooleanMapping.fromYaml(String key, YamlMap? source) {
    if (source == null) {
      throw ArgumentError.notNull('source');
    }

    final variableName = key;
    final folders = (source.nodes[_foldersKey] as YamlList?)
            ?.map(
              (element) =>
                  BooleanNameSingleMapping.fromYaml(element as YamlMap?),
            )
            .toList() ??
        [];
    final files = (source.nodes[_filesKey] as YamlList?)
            ?.map(
              (element) =>
                  BooleanNameSingleMapping.fromYaml(element as YamlMap?),
            )
            .toList() ??
        [];

    return BooleanMapping(
      booleanName: variableName,
      folders: folders,
      files: files,
    );
  }

  String booleanName;
  List<BooleanNameSingleMapping> folders;
  List<BooleanNameSingleMapping> files;

  BooleanMapping copyWith({
    String? booleanName,
    List<BooleanNameSingleMapping>? folders,
    List<BooleanNameSingleMapping>? files,
  }) =>
      BooleanMapping(
        booleanName: booleanName ?? this.booleanName,
        folders: folders ?? this.folders,
        files: files ?? this.files,
      );

  @override
  String toString() => 'BooleanMapping(booleanName: $booleanName, '
      'folders: $folders, files: $files)';
}
