import {Code, PerformanceCompact, PerformanceFull, UserFull} from "@/types/kmusical"; import {CodeGroup} from "@/query/code"; import {getMonitoringDisplayRow, getMonitoringFilterValue} from "@/lib/kmusical/performance-status"; export const ROLE = { ADMIN: 1001, RESEARCHER: 1002, FIELD_INSPECTOR: 1003, CONTACT_AGENT: 1004, FOUNDATION: 2011, MONITORING: 9003, } as const; export const getRoleGroup = (role?: number | null) => { if (!role) return 0; return Math.floor(role / 1000) * 1000; }; export const isResearcherRole = (role?: number | null) => !role ? false : role <= ROLE.RESEARCHER; export const isInternalRole = (role?: number | null) => getRoleGroup(role) === 1000; export const isExternalRole = (role?: number | null) => getRoleGroup(role) === 2000; export const isMonitoringRole = (role?: number | null) => getRoleGroup(role) === 9000; export const isFoundationRole = (role?: number | null) => role === ROLE.FOUNDATION; export const getCodeLabel = ( codes: Partial>, group: CodeGroup, value: number | string | undefined | null ) => { if (value === undefined || value === null || value === "") return ""; const numericValue = Number(value); const found = codes[group]?.find((code) => code.codeValue === numericValue); return found?.codeLabel ?? String(value); }; export const getDetailValue = ( detail: Record | undefined, key: string, fallback = "" ) => { if (!detail) return fallback; const value = detail[key]; if (value !== undefined && value !== "") return value; return fallback; }; export const includesText = (value: unknown, keyword: string) => { if (!keyword) return true; return String(value ?? "").toLowerCase().includes(keyword.toLowerCase()); }; export type PerformanceFilterState = { businessType: string[]; area: string[]; month: string; monitoringDate: string; slotStatus: Record; keyword: string; }; export const filterPerformance = ( item: PerformanceCompact | PerformanceFull, filters: Partial ) => { const keyword = filters.keyword?.trim() ?? ""; const schedule = item.performanceMonitoringSchedule; const matches = (filter: string | string[] | undefined, value: number | string) => { if (!filter || filter.length === 0) return true; const values = Array.isArray(filter) ? filter : [filter]; return values.includes(String(value)); }; const matchesSlotStatus = Object.entries(filters.slotStatus ?? {}).every( ([reviewSlot, status]) => !status || getMonitoringFilterValue(getMonitoringDisplayRow(item, Number(reviewSlot))) === status ); return ( matches(filters.businessType, item.businessType) && matches(filters.area, item.performanceArea) && matches(filters.month, item.performanceStartMonth) && (!filters.monitoringDate || schedule?.monitoringDate === filters.monitoringDate) && matchesSlotStatus && ( includesText(item.performanceId, keyword) || includesText(item.performanceName, keyword) || includesText(item.artistName, keyword) ) ); }; export const filterUser = ( user: UserFull, filters: { area?: string | string[]; gender?: string | string[]; genre?: string | string[]; keyword?: string; } ) => { const detail = user.userDetail ?? {}; const area = getDetailValue(detail, "userArea"); const gender = getDetailValue(detail, "userGender"); const genre = getDetailValue(detail, "userGenre"); const keyword = filters.keyword?.trim() ?? ""; const matches = (filter: string | string[] | undefined, value: string) => { if (!filter || filter.length === 0) return true; const values = Array.isArray(filter) ? filter : [filter]; return values.includes(value); }; return ( matches(filters.area, area) && matches(filters.gender, gender) && matches(filters.genre, genre) && ( includesText(user.userId, keyword) || includesText(user.userName, keyword) || includesText(user.userTeam, keyword) || includesText(user.userCmpn, keyword) || includesText(user.memo, keyword) || includesText(getDetailValue(detail, "userTel"), keyword) || includesText(getDetailValue(detail, "userEmail"), keyword) ) ); };