import { getPinApi, resetPinApi } from "./serve.js";
import dayjs from "dayjs";
import { useState } from "react";
import { useRequest } from "ahooks";
//#region src/components/pinModal/hooks.ts
const usePin = () => {
	const [pin, setPin] = useState("");
	const [lastUpdateTime, setLastUpdateTime] = useState("");
	const _getPinRequest = useRequest(getPinApi, {
		manual: true,
		loadingDelay: 100,
		onSuccess: (res) => {
			const { code, data } = res;
			if (code === 200 && (data === null || data === void 0 ? void 0 : data.pin)) {
				setPin(data.pin);
				setLastUpdateTime(getTime((data === null || data === void 0 ? void 0 : data.generated_at) || ""));
			}
		}
	});
	const getTime = (time) => {
		if (!time) return "";
		return dayjs(time).format("YYYY-MM-DD HH:mm");
	};
	const _resetPinRequest = useRequest(resetPinApi, {
		manual: true,
		loadingDelay: 100,
		onSuccess: (res) => {
			const { code, data } = res;
			if (code === 200 && (data === null || data === void 0 ? void 0 : data.pin)) {
				setPin(data.pin);
				setLastUpdateTime(getTime((data === null || data === void 0 ? void 0 : data.generated_at) || ""));
			}
		}
	});
	const getPin = (id) => {
		_getPinRequest.run({ wallet_detail_id: id });
	};
	const resetPin = (id) => {
		_resetPinRequest.run({ wallet_detail_id: id });
	};
	return {
		pin,
		lastUpdateTime,
		getPin,
		resetPin,
		resetPinLoading: _resetPinRequest.loading
	};
};
//#endregion
export { usePin };
