import { useCodeMap } from './CodeProvider' import type { Code } from '../api/client' /** * 그룹의 선택지. 서버가 정렬순서대로, 사용 중인 것만 내려준다. * 없는 그룹이면 빈 배열이라 화면이 터지지는 않는다. */ export function useCodes(groupId: string): Code[] { return useCodeMap()[groupId] ?? [] } /** * 쓰이는 자리(scope)로 거른 선택지. 공공누리 유형이 이걸 쓴다 - * 같은 코드체계라도 변호사 판정(review)·권리처리 판정(process)·기존 부착(prior)에 * 나오는 목록이 서로 다르다. */ export function useScopedCodes(groupId: string, scope: string): Code[] { return useCodes(groupId).filter((code) => scopes(code).includes(scope)) } /** 라디오 첫 줄에 놓을 것과 둘째 줄에 놓을 것으로 나눈다. */ export function useSplitByFirstRow( groupId: string, scope: string, ): { first: Code[]; rest: Code[] } { const codes = useScopedCodes(groupId, scope) return { first: codes.filter((code) => firstRow(code).includes(scope)), rest: codes.filter((code) => !firstRow(code).includes(scope)), } } /** * 저장된 값의 화면 표시명. 코드표에 없는 값이면 값 자체를 그대로 돌려준다 - * 과거 데이터가 화면에서 빈칸으로 사라지는 것보다 원문이라도 보이는 편이 낫다. */ export function useCodeLabel(groupId: string, code: string | null | undefined): string { const found = useCodes(groupId).find((candidate) => candidate.code === code) return found?.label ?? code ?? '' } /** 코드 한 건. 없으면 undefined. */ export function useCode(groupId: string, code: string | null | undefined): Code | undefined { return useCodes(groupId).find((candidate) => candidate.code === code) } function stringList(code: Code, key: string): string[] { const raw = code.attrs[key] return Array.isArray(raw) ? (raw as string[]) : [] } export function scopes(code: Code): string[] { return stringList(code, 'scopes') } export function firstRow(code: Code): string[] { return stringList(code, 'firstRow') } /** attrs의 문자열 속성. 없으면 null. */ export function attr(code: Code | undefined, key: string): string | null { const value = code?.attrs[key] return value == null ? null : String(value) } /** attrs의 불리언 속성. 없으면 false. */ export function flag(code: Code | undefined, key: string): boolean { return code?.attrs[key] === true }