"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewsProvider = void 0;
const url_join_1 = __importDefault(require("url-join"));
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
const util_1 = require("../../util");
const abstract_content_provider_1 = require("../abstract-content-provider");
const data_accessor_1 = require("../data-accessor/data-accessor");
/** @internal */
class NewsProvider extends abstract_content_provider_1.AbstractContentProvider {
    constructor(session, tableBuilder) {
        super(session);
        this.tableBuilder = tableBuilder;
        this.apiGroup = 'data';
        this.endpointName = 'news';
        this.dataAccessor = new data_accessor_1.DataAccessorImpl(session);
    }
    async getStory(params) {
        (0, util_1.validateRequired)(Object.assign({}, params), ['storyId'], 'News.Story.Params');
        const { storyId, extendedParams } = params, rest = __rest(params, ["storyId", "extendedParams"]);
        const requestParams = {
            url: (0, url_join_1.default)(this.getEndpointPath('stories'), storyId),
            dataAccessorContentQuery: Object.assign(Object.assign({}, rest), extendedParams),
        };
        const response = await this.dataAccessor.getData(requestParams);
        const contentResponse = this.toContentResponse(response);
        return this.addNewsStoryToContentResponse(response.data, contentResponse);
    }
    async getHeadlines(params) {
        (0, util_1.validateRequired)(Object.assign({}, params), ['query'], 'News.Headlines.Params');
        const { extendedParams, count: limit } = params, query = __rest(params, ["extendedParams", "count"]);
        const requestContent = Object.assign(Object.assign(Object.assign({}, query), { limit }), extendedParams);
        const requestParams = {
            url: this.getEndpointPath('headlines'),
            dataAccessorContentQuery: requestContent,
        };
        const response = await this.dataAccessor.getData(requestParams);
        const buildTable = () => this.tableBuilder.build(response.data);
        return this.toContentResponse(response, buildTable);
    }
    addNewsStoryToContentResponse(responseData, contentResponse) {
        const { itemMeta, contentMeta } = responseData.newsItem;
        const formattedResponse = (0, cloneDeep_1.default)(contentResponse);
        formattedResponse.data.news = {
            story: {
                title: itemMeta.title[0].$,
                creator: contentMeta.creator[0]._qcode,
                source: contentMeta.infoSource,
                language: contentMeta.language,
                subject: contentMeta.subject,
                itemCodes: contentMeta.subject.map((subject) => subject._qcode),
                urgency: contentMeta.urgency.$,
                creationDate: itemMeta.firstCreated.$,
                updateDate: itemMeta.versionCreated.$,
                raw: responseData,
                newsType: 'story',
            },
        };
        return formattedResponse;
    }
}
exports.NewsProvider = NewsProvider;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmV3cy1wcm92aWRlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9jb250ZW50L25ld3MvbmV3cy1wcm92aWRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLHdEQUErQjtBQUMvQixpRUFBeUM7QUFHekMscUNBQThDO0FBQzlDLDRFQUF1RTtBQUd2RSxrRUFBa0U7QUFNbEUsZ0JBQWdCO0FBQ2hCLE1BQWEsWUFBNEMsU0FBUSxtREFBdUI7SUFLcEYsWUFBWSxPQUFnQixFQUFVLFlBQThDO1FBQ2hGLEtBQUssQ0FBQyxPQUFPLENBQUMsQ0FBQztRQURtQixpQkFBWSxHQUFaLFlBQVksQ0FBa0M7UUFKN0UsYUFBUSxHQUF1QixNQUFNLENBQUM7UUFDdEMsaUJBQVksR0FBRyxNQUFNLENBQUM7UUFLekIsSUFBSSxDQUFDLFlBQVksR0FBRyxJQUFJLGdDQUFnQixDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQ3RELEN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