// 파일 최상단 상수 — KOSHA 산업안전포털 안전보건자료실 Live API 클라이언트
// 비로그인 endpoint 검증 완료 (2026-04-25): 평균 0.298s, rate limit 없음
// 출처: https://portal.kosha.or.kr/archive/cent-archive/master-arch
// 8,976건 카탈로그 (OPS 3,629 / 동영상 803 / 책자 1,133 / 교안 486 / 기타 2,925)
const API_BASE = "https://portal.kosha.or.kr";
const ENDPOINTS = {
    list: `${API_BASE}/api/portal24/bizV/p/VCPDG01007/selectMediaList`,
    fileList: `${API_BASE}/api/portal24/bizA/p/files/getFileList`,
    codes: `${API_BASE}/api/portal24/bizA/p/ACMCA02001/codes`,
};
const COMMON_HEADERS = {
    "Content-Type": "application/json",
    Accept: "application/json, text/plain, */*",
    chnlid: "portal24",
    Referer: "https://portal.kosha.or.kr/archive/cent-archive/master-arch",
    "User-Agent": "agent-safety-oss/0.1 (+https://github.com/ratelworks/agent-safety-oss)",
};
// shpCd 매핑 — 콘텐츠 형식 코드
export const SHP_CODE_MAP = {
    ops: { code: "12", ko: "OPS" },
    video: { code: "02", ko: "동영상" },
    book: { code: "01", ko: "책자" },
    ppt: { code: "07", ko: "교안(PPT)" },
    other: { code: "10", ko: "기타" },
};
// 5개 facet groupId — ACMCA02001/codes 호출용
export const FACET_GROUP_IDS = {
    ctgr01: { groupId: "CONTS_GNRL_CD", ko: "일반분야" },
    ctgr02: { groupId: "CONTS_MCHN_FXTR_REL_CD", ko: "기계설비" },
    ctgr03: { groupId: "CONTS_JOB_REL_CD", ko: "작업" },
    ctgr04: { groupId: "CONTS_CR_HLTH_REL_CD", ko: "직업건강" },
    ctgr05: { groupId: "CONTS_SPCL_SHP_CD", ko: "특수형태" },
};
// ─── 공통 fetch (AbortController 타임아웃) ───
const DEFAULT_TIMEOUT_MS = 30_000;
async function postJson(url, body, timeoutMs = DEFAULT_TIMEOUT_MS) {
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), timeoutMs);
    try {
        const res = await fetch(url, {
            method: "POST",
            headers: COMMON_HEADERS,
            body: JSON.stringify(body),
            signal: controller.signal,
        });
        if (!res.ok) {
            throw new Error(`KOSHA API ${res.status} ${res.statusText} — ${url}`);
        }
        return (await res.json());
    }
    finally {
        clearTimeout(timer);
    }
}
// ─── selectMediaList ───
export async function searchArchive(input) {
    const body = {
        shpCd: input.shpCd ?? "12", // 기본 OPS
        searchCondition: "all",
        searchValue: input.searchValue ?? null,
        ascDesc: "desc",
        ctgr01: input.ctgr01 ? 1 : 0,
        arrCtgrStr01: input.ctgr01 ?? "",
        ctgr02: input.ctgr02 ? 1 : 0,
        arrCtgrStr02: input.ctgr02 ?? "",
        ctgr03: input.ctgr03 ? 1 : 0,
        arrCtgrStr03: input.ctgr03 ?? "",
        ctgr04: input.ctgr04 ? 1 : 0,
        arrCtgrStr04: input.ctgr04 ?? "",
        ctgr05: input.ctgr05 ? 1 : 0,
        arrCtgrStr05: input.ctgr05 ?? "",
        startDt: null,
        endDt: null,
        page: input.page ?? 1,
        rowsPerPage: input.rowsPerPage ?? 10,
    };
    const resp = await postJson(ENDPOINTS.list, body);
    const list = resp.payload?.list ?? [];
    const total = list.length > 0 ? (list[0].totalCount ?? 0) : 0;
    return {
        total,
        page: body.page,
        rowsPerPage: body.rowsPerPage,
        items: list,
    };
}
// ─── getFileList (첨부 bundle 의 파일 목록) ───
export async function getArchiveFiles(contsAtcflNo) {
    const resp = await postJson(ENDPOINTS.fileList, {
        fileId: contsAtcflNo,
        fileUploadType: "02",
        isDirect: "N",
    });
    return resp.payload ?? [];
}
// ─── ACMCA02001/codes (facet 코드 목록) ───
export async function getFacetCodes(groupId) {
    const resp = await postJson(ENDPOINTS.codes, { groupId });
    return (resp.payload ?? []).map((p) => ({ code: p.code, name: p.name }));
}
// 다운로드 URL 조립 (downloadAtchFile)
export function buildDownloadUrl(atcflNo, atcflSeq, fileName, mimeType = "application/pdf") {
    const enc = encodeURIComponent(fileName);
    return `${API_BASE}/api/portal24/bizA/p/files/downloadAtchFile?atcflNo=${atcflNo}&atcflSeq=${atcflSeq}&isDirect=N&taskSeCd=&fileName=${enc}&mimeType=${mimeType}`;
}
//# sourceMappingURL=kosha-archive-client.js.map