import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_dayjs__ from "dayjs";
import * as __WEBPACK_EXTERNAL_MODULE__date_picker_index_mjs__ 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 (0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(timestamp);
}
function getTimestamp(date) {
    let accuracy = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 'millisecond';
    if ('second' === accuracy) return (0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date).unix();
    return (0, __WEBPACK_EXTERNAL_MODULE_dayjs__["default"])(date).valueOf();
}
const UnixTimestamp = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { value, onChange, accuracy } = props;
    const v = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>toValue(value, accuracy), [
        value
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__date_picker_index_mjs__.DatePicker, {
        ...props,
        value: v,
        onChange: (v)=>{
            if (onChange) onChange(getTimestamp(v, accuracy));
        }
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)((props)=>{
    const { value, accuracy } = props;
    const v = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>toValue(value, accuracy), [
        value
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__date_picker_index_mjs__.DatePicker.ReadPretty, {
        ...props,
        value: v
    });
}));
export { UnixTimestamp };
