"use client";
import { Button } from "@suphark/ui/components/shared/button/button";
import {
compareMonth,
currentMonthKey,
formatMonthKey,
type MonthKey,
shiftMonth,
} from "@suphark/ui/lib/month";
import { cn } from "@suphark/ui/lib/utils";
import { CalendarDays, ChevronLeft, ChevronRight } from "lucide-react";
import { useCallback, useState } from "react";
import { useUiText } from "./ui-text";
export interface MonthStepperProps {
/** เดือนที่เลือก `"YYYY-MM"` */
value: MonthKey;
onValueChange: (value: MonthKey) => void;
/** เดือนแรก/สุดท้ายที่เลือกได้ (ปุ่ม ‹ › ปิดเมื่อถึงขอบ) */
min?: MonthKey;
max?: MonthKey;
/** `th-TH` = พ.ศ. (ค่าเริ่มต้น), `th-TH-u-ca-gregory` = ไทย ค.ศ., `en-US` */
locale?: string;
/** ชื่อเดือนเต็ม/ย่อ (มือถือแคบ ๆ ใช้ short) */
format?: "long" | "short";
/** ข้อความกลางเอง (เช่น "รอบบิล ต.ค. 69") — ไม่ส่ง = ชื่อเดือนตาม locale */
formatLabel?: (value: MonthKey) => string;
/** กดชื่อเดือนแล้วกลับเดือนปัจจุบัน (ค่าเริ่มต้น true) */
resetToCurrent?: boolean;
className?: string;
}
/** "เดือนนี้" ของ MonthStepper/useMonthCursor คิดตามเวลาไทย — แอปของเราใช้ในไทย และต้องได้ค่าเดียวกันทั้ง server และ browser */
const APP_TIME_ZONE = "Asia/Bangkok";
/**
* เลือกเดือนแบบ ‹ เดือน › — กดชื่อเดือนเพื่อกลับเดือนนี้ (ยกจาก DueBell `MonthSelector`)
* ใช้คู่กับ `CalendarMonthGrid month` ได้เลย (รูปแบบ `"YYYY-MM"` เดียวกัน)
*
* const { month, setMonth } = useMonthCursor();
*
*/
export function MonthStepper({
value,
onValueChange,
min,
max,
locale = "th-TH",
format = "long",
formatLabel,
resetToCurrent = true,
className,
}: MonthStepperProps) {
const text = useUiText().month;
const prev = shiftMonth(value, -1);
const next = shiftMonth(value, 1);
const canPrev = !min || compareMonth(prev, min) >= 0;
const canNext = !max || compareMonth(next, max) <= 0;
// โซนเวลาคงที่: server (UTC) กับ browser ได้เดือนเดียวกัน — ไม่งั้นช่วง 00–07 น. วันที่ 1 จะ hydration mismatch
const now = currentMonthKey(undefined, APP_TIME_ZONE);
const nowInRange = (!min || compareMonth(now, min) >= 0) && (!max || compareMonth(now, max) <= 0);
const label = formatLabel ? formatLabel(value) : formatMonthKey(value, locale, format);
const isCurrent = value === now;
// กดชื่อเดือนเพื่อกลับเดือนนี้ได้ก็ต่อเมื่อยังไม่ได้อยู่เดือนนี้ และเดือนนี้อยู่ในช่วงที่เลือกได้
const canReset = resetToCurrent && !isCurrent && nowInRange;
return (
{canReset ? (
) : (
// อยู่เดือนนี้แล้ว (หรือปิด resetToCurrent) = ไม่มีอะไรให้กด — เป็นป้ายชื่อเดือน ไม่ใช่ปุ่มที่ปิดอยู่
// (เดิมเป็น
);
}
/**
* state เดือนปัจจุบัน + ตัวเลื่อน — `initial` ไม่ส่ง = เดือนนี้ (ตามเวลาไทย เพื่อให้ server/browser ตรงกัน)
* ต้องการเก็บใน URL ให้ใช้ `useTableSearchParams`/searchParams ของแอปแทน แล้วส่ง value/onValueChange เอง
*/
export function useMonthCursor(initial?: MonthKey) {
const [month, setMonth] = useState(
() => initial ?? currentMonthKey(undefined, APP_TIME_ZONE),
);
const previous = useCallback(() => setMonth((m) => shiftMonth(m, -1)), []);
const next = useCallback(() => setMonth((m) => shiftMonth(m, 1)), []);
const reset = useCallback(() => setMonth(currentMonthKey(undefined, APP_TIME_ZONE)), []);
return { month, setMonth, previous, next, reset };
}