import dayjs from "dayjs";
import "dayjs/locale/es";
import { create } from "zustand";
dayjs.locale("es");

interface ITimePickerStore {
    visible: boolean;
    timeFraction: number;
    value: dayjs.Dayjs;
    defaultValue: dayjs.Dayjs;
    onAccept: (newDay: dayjs.Dayjs) => void;
    setTimeFraction: (timeFraction: number) => void;
    setValue: (value: dayjs.Dayjs) => void;
    setDefaultValue: (value: dayjs.Dayjs) => void;
    show: (onAccept: (newDay: dayjs.Dayjs) => void) => void;
    hide: () => void;
}

export type TimePickerState = ITimePickerStore;

const useTimePickerStore = create<TimePickerState>()((set) => ({
    visible: false,
    timeFraction: 15,
    value: dayjs(new Date()).hour(8).minute(0),
    defaultValue: dayjs(new Date()),
    onAccept: () => {},
    setTimeFraction(timeFraction) {
        set(() => ({
            timeFraction: timeFraction,
        }));
    },
    setDefaultValue(value) {
        set(() => ({
            defaultValue: value,
        }));
    },
    setValue(value) {
        set(() => ({
            value: value,
        }));
    },
    show: (onAccept: (newDay: dayjs.Dayjs) => void) =>
        set(() => ({
            visible: true,
            onAccept: onAccept,
        })),

    hide: () =>
        set(() => ({
            visible: false,
            onAccept: () => {},
        })),
}));

export { useTimePickerStore };
