import { getIndexOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
import type { Factor, ResIndexDetail } from '@nalssigak/shared/api/client/types.gen';
import { loadOf } from '@nalssigak/shared/lib';
import { useQuery } from '@tanstack/react-query';
import { LinearGradient } from 'expo-linear-gradient';
import { useEffect } from 'react';
import { View } from 'react-native';
import { Text } from '@/components/text';
import Svg, { Circle, Line, Polygon, Text as SvgText, TSpan } from 'react-native-svg';
import { Ic, Skel, StateBox } from '@/components/ui';
import { useRegion } from '@/lib/region';
import { useSky } from '@/lib/sky';
import { Body, useSoft } from './parts';
const SLOT_KEYS = ['morning', 'day', 'evening', 'night'] as const;
const SLOT_NAMES = { morning: '아침', day: '낮', evening: '저녁', night: '밤' } as const;
const N4 = [0, 1, 2, 3];
const N6 = [0, 1, 2, 3, 4, 5];
const labelLines = (label: string) => {
const cut = label.indexOf(' ');
return cut < 0 ? [label] : [label.slice(0, cut), label.slice(cut + 1)];
};
function Radar({ name, factors, size = 250 }: { name: string; factors: Factor[]; size?: number }) {
const night = useSky().kind === 'night';
const c = size / 2;
const r = size / 2 - 40;
const n = factors.length;
const pt = (i: number, v: number) => {
const a = -Math.PI / 2 + (i * 2 * Math.PI) / n;
return [c + Math.cos(a) * r * v, c + Math.sin(a) * r * v] as const;
};
const ring = (v: number) => factors.map((_, i) => pt(i, v).join(',')).join(' ');
return (
);
}
function GakSkel() {
const s = useSoft();
return (
{N4.map((i) => (
))}
{N6.map((i) => (
))}
);
}
export function GakSheet({ indexId, onTitle }: { indexId: number; onTitle: (t: string) => void }) {
const { region } = useRegion();
const s = useSoft();
const q = useQuery(getIndexOptions({ path: { index_id: indexId }, query: { region: region.code } }));
const d: ResIndexDetail | undefined = q.data;
const g = d?.index;
useEffect(() => {
onTitle(g ? `${g.rank}위 ${g.name}` : '계산 근거');
}, [g, onTitle]);
const load = loadOf(q);
if (!g) return
{load === 'loading' ? : q.refetch()} empty="이 각의 계산 근거가 아직 없어요." hold={} boxClassName="inset-0" />};
const factors = d.factors ?? [];
const bands = d.bands ?? [];
const now = bands.find((b) => b.current);
return (
{g.name}
{d.description && {d.description}}
{g.score}
{!g.active && 지금 시간대엔 해당 없어요. 점수는 오늘 가장 좋은 시간대 기준이에요.}
{now && (
{now.low}~{now.high}점{' '}
{now.text}
)}
{factors.length > 2 && }
{g.slots && g.slots.length > 0 && (
{SLOT_KEYS.map((k) => {
const sl = g.slots?.find((x) => x.slot === k);
const cur = g.active && g.slot === k;
return (
{SLOT_NAMES[k]}
{cur && ' · 지금'}
{sl ? sl.score : '–'}
);
})}
)}
{g.trend && g.trend.length > 0 && (
{g.trend.map((t) => (
{t.label}
{t.score}
예상
))}
)}
{bands.length > 0 && (
{bands.map((b) => (
{b.low}~{b.high}
{b.text}
))}
)}
{factors.map((f) => (
{f.label}
비중 {f.weight}%
{f.note}
))}
{d.zero_reason && (
오늘 걸린 조건: {d.zero_reason}
)}
{factors.length > 0 && (
계산 방법: 요소 점수에 비중을 곱해 더하고, 가장 낮은 요소가 낮을수록 최대 25%까지 더 깎아요
)}
{d.zero_rules && (
점수 깎이는 조건: {d.zero_rules}
)}
{d.limit && (
반영 못 한 것: {d.limit}
)}
);
}