export default {
    _prepareContext: (context) => {

        const actorData = context.document
        context.spells = []
        context.system = actorData.system
        context.flags = actorData.flags
        context.derived = context.document.system
        context.originalName = actorData.name
        context.name = context.derived.name ?? actorData.name
        context.effects = actorData.effects ?? []

        const cleanUpMerkmal = (merkmale) => {
            return merkmale.split(",").map((merkmal) => merkmal.trim())
        }

        const prepareEigenschaftRoll = (actorData, name) => {
            if (name && name !== "*") {
                return actorData.system.attribute[name.toLowerCase()].aktuell
            } else {
                return 0
            }
        }

        actorData.itemTypes["Spell"].forEach((item, index) => {

                const eigenschaften = item.system.probe;
                const werte = [
                    {name: eigenschaften[0], value: prepareEigenschaftRoll(actorData, eigenschaften[0])},
                    {name: eigenschaften[1], value: prepareEigenschaftRoll(actorData, eigenschaften[1])},
                    {name: eigenschaften[2], value: prepareEigenschaftRoll(actorData, eigenschaften[2])}
                ]
                context.spells.push({
                    id: item._id,
                    name: item.name,
                    zfw: item.system.zfw,
                    hauszauber: item.system.hauszauber,
                    merkmal: cleanUpMerkmal(item.system.merkmal),
                    rollEigenschaft1: werte[0].value,
                    rollEigenschaft2: werte[1].value,
                    rollEigenschaft3: werte[2].value,
                    eigenschaft1: werte[0].name,
                    eigenschaft2: werte[1].name,
                    eigenschaft3: werte[2].name,
                    fav: item.getFlag("DSA_4-1", "favourite")
                })

        })
        context.hasSpells = context.spells.length > 0

        return context
    },
    _onRender: (context, options) => {

    },
    _getTabConfig: (group, thisObject) => {
        const hasSpells = thisObject.document.items.filter(p => p.type === "Spell").length > 0 ?? false
        if (hasSpells) {
            group.tabs.push({id: "spells", group: "sheet", label: "Zauber"})
        }
    },
    template: `systems/DSA_4-1/templates/actor/character/tab-spells.hbs`
}