import type { IssueMatrixRow } from "./issue-matrix-data";

export type IssueMatrixLanguage = "en" | "kh";

export type IssueMatrixTranslation = {
    title: string;
    subtitle: string;
    search: string;
    exportAsXlsx: string;
    previous: string;
    next: string;
    viewDetail: string;
    download: string;
    noData: string;
    totalIssues: string;
    solved: string;
    inProgress: string;
    notAddressed: string;
    totalPrimaryAgencies: string;
    columns: Partial<Record<keyof IssueMatrixRow, string>>;
};

export function normalizeIssueMatrixLanguage(
    language?: string
): IssueMatrixLanguage {
    if (language === "kh" || language === "km") return "kh";
    return "en";
}

export const issueMatrixText: Record<
    IssueMatrixLanguage,
    IssueMatrixTranslation
> = {
    en: {
        title: "Issue Matrix",
        subtitle: "Issue Matrix",
        search: "Search",
        exportAsXlsx: "Export as xlsx",
        previous: "Previous",
        next: "Next",
        viewDetail: "View Detail",
        download: "Download",
        noData: "No data found",

        totalIssues: "Total Issues",
        solved: "Solved",
        inProgress: "In Progress",
        notAddressed: "Not Addressed",
        totalPrimaryAgencies: "Total Primary Agencies",

        columns: {
            id: "No.",
            workingGroup: "Working Group",
            issue: "Issue",
            category: "Issue Category",
            issueDescription: "Issue Description",
            recommendation: "Recommendation",
            governmentDecision: "Government Decision",
            status: "Status",
            dateOfSolution: "Date of Solution",
            primaryAgency: "Primary Agency",
            meetingType: "Meeting Type",
            wgMeetingDate: "WG Meeting Date",
            secondAgency: "Second Agency",
            thirdAgency: "Third Agency",
            fourthAgency: "Fourth Agency",
            fifthAgency: "Fifth Agency",
            indicator: "Indicator",
            sourceOfVerification: "Source of Verification",
            verificationLink: "Link to Verification Source",
            action: "Action",
        },
    },

    kh: {
        title: "តារាងបញ្ហា",
        subtitle: "តារាងបញ្ហា",
        search: "ស្វែងរក",
        exportAsXlsx: "នាំចេញជា xlsx",
        previous: "មុន",
        next: "បន្ទាប់",
        viewDetail: "មើលលម្អិត",
        download: "ទាញយក",
        noData: "មិនមានទិន្នន័យ",

        totalIssues: "បញ្ហាសរុប",
        solved: "បានដោះស្រាយ",
        inProgress: "កំពុងដំណើរការ",
        notAddressed: "មិនទាន់ដោះស្រាយ",
        totalPrimaryAgencies: "ស្ថាប័នទទួលបន្ទុកសរុប",

        columns: {
            id: "ល.រ",
            workingGroup: "ក្រុមការងារ",
            issue: "បញ្ហា",
            category: "ប្រភេទបញ្ហា",
            issueDescription: "ការពិពណ៌នាបញ្ហា",
            recommendation: "អនុសាសន៍",
            governmentDecision: "សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល",
            status: "ស្ថានភាព",
            dateOfSolution: "កាលបរិច្ឆេទដោះស្រាយ",
            primaryAgency: "ស្ថាប័នទទួលបន្ទុក",
            meetingType: "ប្រភេទកិច្ចប្រជុំ",
            wgMeetingDate: "កាលបរិច្ឆេទកិច្ចប្រជុំក្រុមការងារ",
            secondAgency: "ស្ថាប័នទី២",
            thirdAgency: "ស្ថាប័នទី៣",
            fourthAgency: "ស្ថាប័នទី៤",
            fifthAgency: "ស្ថាប័នទី៥",
            indicator: "សូចនាករ",
            sourceOfVerification: "ប្រភពផ្ទៀងផ្ទាត់",
            verificationLink: "តំណភ្ជាប់ប្រភពផ្ទៀងផ្ទាត់",
            action: "សកម្មភាព",
        },
    },
};

const khmerValueLabels: Record<string, string> = {
    Draft: "ព្រាង",
    "New Submission": "បានដាក់ស្នើថ្មី",
    "In Progress": "កំពុងដំណើរការ",
    Solved: "បានដោះស្រាយ",
    "Not Addressed": "មិនទាន់ដោះស្រាយ",

    Download: "ទាញយក",

    "Working Group": "ក្រុមការងារ",
    "All Status": "ស្ថានភាពទាំងអស់",
    Category: "ប្រភេទ",
    "Primary Agency": "ស្ថាប័នទទួលបន្ទុក",
    Year: "ឆ្នាំ",
    "Progress Report": "របាយការណ៍វឌ្ឍនភាព",
    Attachment: "ឯកសារភ្ជាប់",
    "Has Attachment": "មានឯកសារភ្ជាប់",
    "No Attachment": "មិនមានឯកសារភ្ជាប់",
    Yes: "បាទ/ចាស",
    No: "ទេ",

    Policy: "គោលនយោបាយ",
    Procedure: "នីតិវិធី",
    Legislation: "ច្បាប់",
    Market: "ទីផ្សារ",
    Strategy: "យុទ្ធសាស្ត្រ",
    Governance: "អភិបាលកិច្ច",
    Taxation: "ពន្ធដារ",
    Trade: "ពាណិជ្ជកម្ម",
    Government: "រាជរដ្ឋាភិបាល",
    "Human Resource": "ធនធានមនុស្ស",

    "Not available": "មិនមាន",
    "Not applicable": "មិនពាក់ព័ន្ធ",
    "To be provided": "នឹងផ្តល់ជូន",
};

export function translateIssueMatrixValue(
    value: string | number | boolean | null | undefined,
    language: IssueMatrixLanguage
) {
    if (value === null || value === undefined) return "";

    const textValue = String(value);

    if (language === "en") return textValue;

    return khmerValueLabels[textValue] ?? textValue;
}
