package thb.jeanluc.adventure.loader.dto;

import thb.jeanluc.adventure.loader.WorldLoadException;
import thb.jeanluc.adventure.model.Condition;

import java.util.ArrayList;
import java.util.List;

/** YAML condition: exactly one of the fields is set. */
public record ConditionDto(String flag, String notFlag, String hasItem) {

    public Condition toModel() {
        if (flag != null) {
            return new Condition(Condition.Type.FLAG, flag);
        }
        if (notFlag != null) {
            return new Condition(Condition.Type.NOT_FLAG, notFlag);
        }
        if (hasItem != null) {
            return new Condition(Condition.Type.HAS_ITEM, hasItem);
        }
        throw new WorldLoadException("Condition must set one of flag/notFlag/hasItem");
    }

    public static List<Condition> toModelList(List<ConditionDto> dtos) {
        List<Condition> out = new ArrayList<>();
        if (dtos != null) {
            for (ConditionDto d : dtos) {
                out.add(d.toModel());
            }
        }
        return out;
    }
}
