"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IccCodeXApi = void 0;
const icc_api_1 = require("../icc-api");
const codelng_1 = require("./rsrc/codelng");
const icd10_1 = require("./rsrc/icd10");
const icpc2_1 = require("./rsrc/icpc2");
const utils_1 = require("./utils");
const AuthenticationProvider_1 = require("./auth/AuthenticationProvider");
class IccCodeXApi extends icc_api_1.IccCodeApi {
    constructor(host, headers, authenticationProvider = new AuthenticationProvider_1.NoAuthenticationProvider(), fetchImpl = typeof window !== 'undefined'
        ? window.fetch
        : typeof self !== 'undefined'
            ? self.fetch
            : fetch) {
        super(host, headers, authenticationProvider, fetchImpl);
        this.icd10 = icd10_1.default;
        this.icpc2 = icpc2_1.default;
        this.codeLanguages = codelng_1.default;
    }
    /**
     * Groups a list of ICD-10 codes by their corresponding ICD-10 chapter.
     * @param listOfCodes an array of ICD-10 code strings to classify into chapters.
     * @return a sorted array of chapter objects, each containing a code, description, and list of matching sub-codes.
     */
    // noinspection JSUnusedGlobalSymbols
    icdChapters(listOfCodes) {
        return Promise.resolve((0, utils_1.icdChapters)(listOfCodes, this.icd10));
    }
    /**
     * Groups a list of ICPC-2 codes by their corresponding ICPC-2 chapter.
     * @param listOfCodes an array of ICPC-2 code strings to classify into chapters.
     * @return a sorted array of chapter objects, each containing a code, description, and list of matching sub-codes.
     */
    // noinspection JSUnusedGlobalSymbols
    icpcChapters(listOfCodes) {
        return Promise.resolve((0, utils_1.icpcChapters)(listOfCodes, this.icpc2));
    }
    /**
     * Returns the appropriate language code for a given code type. Falls back to French ('fr') if the
     * requested language is not available for the specified type.
     * @param type the code type to look up available languages for.
     * @param lng the desired language code.
     * @return the requested language code if available, or 'fr' as a fallback.
     */
    // noinspection JSUnusedGlobalSymbols
    languageForType(type, lng) {
        const availableLanguages = this.codeLanguages[type];
        return availableLanguages && availableLanguages.indexOf(lng) >= 0 ? lng : 'fr';
    }
    /**
     * Normalizes a code, ensuring it has a consistent structure with id, type, code, and version fields.
     * Accepts either a pipe-delimited string (e.g. "type|code|version") or a Code object.
     * @param c a code represented as a pipe-delimited string or a Code object.
     * @return an object with id, type, code, and version fields fully populated.
     */
    // noinspection JSMethodCanBeStatic, JSUnusedGlobalSymbols
    normalize(c) {
        return typeof c === 'string'
            ? {
                id: c,
                type: c.split(/\|/)[0],
                code: c.split(/\|/)[1],
                version: c.split(/\|/)[2],
            }
            : c.type && c.code && !c.id
                ? {
                    id: c.type + '|' + c.code + '|' + (c.version || '1'),
                    type: c.type,
                    code: c.code,
                    version: c.version || '1',
                }
                : c.id && (!c.code || !c.type || !c.version)
                    ? {
                        id: c.id,
                        type: c.id.split(/\|/)[0],
                        code: c.id.split(/\|/)[1],
                        version: c.id.split(/\|/)[2],
                    }
                    : {
                        id: c.id,
                        type: c.type,
                        code: c.code,
                        version: c.version || '1',
                    };
    }
}
exports.IccCodeXApi = IccCodeXApi;
//# sourceMappingURL=icc-code-x-api.js.map