"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchItemJobRecords = fetchItemJobRecords;
const HubError_1 = require("../HubError");
const jobRecordType_1 = require("./enums/jobRecordType");
/**
 * Fetches job records for a specific item.
 *
 * @param id - The ID of the item.
 * @param options - The options for fetching job records.
 * @returns A promise that resolves to an array of job records.
 * @throws {HubError} If any unimplemented options are provided.
 */
async function fetchItemJobRecords(id, options) {
    const unimplementedOptions = [
        "types",
        "statuses",
    ];
    if (unimplementedOptions.some((option) => options[option])) {
        throw new HubError_1.default("fetchItemJobRecords", `The following options are not yet implemented: ${unimplementedOptions.join(", ")}`);
    }
    const { context } = options;
    const requestUrl = `${context.hubUrl}/api/download/v1/items/${id}/errors${getQueryString(options)}`;
    const { errors: rawErrors } = await fetch(requestUrl).then((response) => response.json());
    // TODO: account for additional row types once the API supports them
    return rawErrors.map(rowToDownloadJobRecord);
}
function getQueryString(options) {
    const params = new URLSearchParams();
    // if you are updating this file, remove this comment and address the lint error
    /* eslint-disable @typescript-eslint/no-unused-expressions */
    options.from && params.append("fromDate", options.from);
    options.to && params.append("toDate", options.to);
    options.limit && params.append("limit", options.limit.toString());
    /* eslint-enable @typescript-eslint/no-unused-expressions */
    const result = params.toString();
    return result && `?${result}`;
}
function rowToDownloadJobRecord(row) {
    return {
        type: jobRecordType_1.JobRecordType.DOWNLOAD,
        message: row.message,
        created: new Date(row.timestamp).getTime(),
        layerId: row.layerId,
        // implement the rest of the fields once the API supports them
        id: null,
        status: null,
        messageId: null,
        modified: null,
    };
}
//# sourceMappingURL=fetchItemJobRecords.js.map