import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { connect, mapReadPretty } from "@tachybase/schema";
import dayjs from "dayjs";
import { DatePicker } from "../date-picker/index.mjs";
const toValue = (value, accuracy)=>{
    if (value) return timestampToDate(value, accuracy);
    return null;
};
function timestampToDate(timestamp) {
    let accuracy = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 'millisecond';
    if ('second' === accuracy) timestamp *= 1000;
    return dayjs(timestamp);
}
function getTimestamp(date) {
    let accuracy = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 'millisecond';
    if ('second' === accuracy) return dayjs(date).unix();
    return dayjs(date).valueOf();
}
const UnixTimestamp = connect((props)=>{
    const { value, onChange, accuracy } = props;
    const v = useMemo(()=>toValue(value, accuracy), [
        value
    ]);
    return /*#__PURE__*/ jsx(DatePicker, {
        ...props,
        value: v,
        onChange: (v)=>{
            if (onChange) onChange(getTimestamp(v, accuracy));
        }
    });
}, mapReadPretty((props)=>{
    const { value, accuracy } = props;
    const v = useMemo(()=>toValue(value, accuracy), [
        value
    ]);
    return /*#__PURE__*/ jsx(DatePicker.ReadPretty, {
        ...props,
        value: v
    });
}));
export { UnixTimestamp };
