package net.minecraft.server.dialog;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Optional;
import net.minecraft.core.HolderSet;
import net.minecraft.util.ExtraCodecs;

public record DialogListDialog(
    @Override CommonDialogData common, HolderSet<Dialog> dialogs, @Override Optional<ActionButton> exitAction, @Override int columns, int buttonWidth
) implements ButtonListDialog {
    public static final MapCodec<DialogListDialog> MAP_CODEC = RecordCodecBuilder.mapCodec(
        instance -> instance.group(
                CommonDialogData.MAP_CODEC.forGetter(DialogListDialog::common),
                Dialog.LIST_CODEC.fieldOf("dialogs").forGetter(DialogListDialog::dialogs),
                ActionButton.CODEC.optionalFieldOf("exit_action").forGetter(DialogListDialog::exitAction),
                ExtraCodecs.POSITIVE_INT.optionalFieldOf("columns", 2).forGetter(DialogListDialog::columns),
                WIDTH_CODEC.optionalFieldOf("button_width", 150).forGetter(DialogListDialog::buttonWidth)
            )
            .apply(instance, DialogListDialog::new)
    );

    @Override
    public MapCodec<DialogListDialog> codec() {
        return MAP_CODEC;
    }
}
