import { useState } from 'react';

import { createModel } from '../utils/model/createModel';

interface UserState {
    name: string;
    age: number;
}

interface UserModel extends UserState {
    updateName: (name: string) => void;
    updateAge: (age: number) => void;
    incrementAge: () => void;
}

const useUserModel = (): UserModel => {
    const [state, setState] = useState<UserState>({
        name: 'John Doe',
        age: 30,
    });

    const updateName = (name: string) => {
        setState((prev) => ({ ...prev, name }));
    };

    const updateAge = (age: number) => {
        setState((prev) => ({ ...prev, age }));
    };

    const incrementAge = () => {
        setState((prev) => ({ ...prev, age: prev.age + 1 }));
    };

    return {
        ...state,
        updateName,
        updateAge,
        incrementAge,
    };
};

export const userModel = createModel(useUserModel, 'user');
