import { defineStore } from 'pinia'

export const useCalculatorStore = defineStore('calculator', () => {
    const tempStore = useTempStore()

    // Initialisierung aus dem TempStore
    const isOpen = ref(false)
    const display = computed({
        get: () => tempStore.settings?.calculator?.display || '0',
        set: (val) => tempStore.modifySettings('calculator', { ...tempStore.settings.calculator, display: val })
    })

    const memory = computed({
        get: () => tempStore.settings?.calculator?.memory || 0,
        set: (val) => tempStore.modifySettings('calculator', { ...tempStore.settings.calculator, memory: val })
    })

    const history = computed({
        get: () => tempStore.filters?.calculator?.history || [],
        set: (val) => tempStore.modifyFilter('calculator', 'history', val)
    })

    function toggle() {
        isOpen.value = !isOpen.value
    }

    function addHistory(expression: string, result: string) {
        const newHistory = [{ expression, result }, ...history.value].slice(0, 10)
        history.value = newHistory
    }

    return {
        isOpen,
        display,
        memory,
        history,
        toggle,
        addHistory
    }
})