import { reactive, toRefs, ComputedRef, UnwrapNestedRefs, ToRefs } from 'vue'

/**
 * 状态存储
 */
export interface Store<T> {
    state: UnwrapNestedRefs<T>;
    getters?: { [propName: string]: ComputedRef };
    actions?: { [propName: string]: (...args: never[]) => unknown };
    methods?: {
        $setData: (callback: (state: UnwrapNestedRefs<T>) => void) => void;
        $storeToRefs: () => ToRefs<UnwrapNestedRefs<T>>;
    }
}

/**
 * 状态存储控制类
 */
export abstract class VueStore<T extends object> implements Store<T>{
    constructor(state: T) {
        this.state = reactive<T>(state)
    }

    state
    getters = {}
    actions = {}
    methods = {
        $setData: (callback: (state: UnwrapNestedRefs<T>) => void) => {
            callback(this.state)
        },
        $storeToRefs: () => {
            return toRefs(this.state)
        },
    }
}

/**
 * 创建状态管理
 * @param store
 * @returns
 */
// export function createStore<T extends object>(store: VueStore<T>) {
//     return shallowReadonly({
//         ...toRefs(store.state),
//         ...store.getters,
//         ...store.actions,
//         ...store.methods,
//     })
// }