package common.village;

import common.item.Item;
import common.item.ItemStack;
import common.tags.TagObject;

public record MerchantRecipe(ItemStack first, ItemStack second, ItemStack result) {
	public MerchantRecipe(TagObject tag) {
		this(ItemStack.readFromTag(tag.getObject("buy")), tag.hasObject("buyB") ? ItemStack.readFromTag(tag.getObject("buyB")) : null, ItemStack.readFromTag(tag.getObject("sell")));
	}

	public MerchantRecipe(ItemStack buy, ItemStack sell) {
		this(buy, null, sell);
	}

	public MerchantRecipe(ItemStack buy, Item sell) {
		this(buy, new ItemStack(sell));
	}

	public boolean both() {
		return this.second != null;
	}

	public TagObject toTags() {
		TagObject tag = new TagObject();
		tag.setObject("buy", this.first.writeTags(new TagObject()));
		tag.setObject("sell", this.result.writeTags(new TagObject()));
		if(this.second != null)
			tag.setObject("buyB", this.second.writeTags(new TagObject()));
		return tag;
	}
}
