'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var parseUrl = require('parseurl');
var UserReports = require('../types/UserReports.js');

class UserStoreItemBuilder {
    userStoreItem;
    requestFormData;
    constructor(reportData, requestFormData) {
        this.requestFormData = requestFormData || {};
        this.userStoreItem = this.addReportData(reportData);
    }
    build = () => {
        return this.userStoreItem;
    };
    addReportData = ({ type, reportId, reportName, description, id, name }) => {
        return {
            type: type,
            reportId,
            reportName,
            description,
            id,
            name,
            timestamp: {},
        };
    };
    addExecutionData = (executionData) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            ...executionData,
        };
        return this;
    };
    addChildExecutionData = (childExecutionData) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            childExecutionData,
        };
        return this;
    };
    addFilters = (filterData) => {
        const filtersQueryString = new URLSearchParams(filterData).toString();
        this.userStoreItem = {
            ...this.userStoreItem,
            ...{
                filters: {
                    data: filterData,
                    queryString: filtersQueryString,
                },
            },
        };
        return this;
    };
    addSortData = (sortData) => {
        const sortByQueryString = new URLSearchParams(sortData).toString();
        this.userStoreItem = {
            ...this.userStoreItem,
            ...{
                sortBy: {
                    data: sortData,
                    queryString: sortByQueryString,
                },
            },
        };
        return this;
    };
    addRequestUrls = (req) => {
        const url = new URL(req.originalUrl, `${req.protocol}://${req.get('host')}`);
        const { pathname, search, origin } = url;
        const { executionId, dataProductDefinitionsPath, dpdPathFromQuery } = this.userStoreItem;
        // Polling path
        let pollingPath = req.baseUrl.replace('/filters', `/${executionId}/status`);
        if (dpdPathFromQuery) {
            pollingPath = `${pollingPath}?dataProductDefinitionsPath=${dataProductDefinitionsPath}`;
        }
        const requestFullUrl = `${origin}${req.originalUrl}`;
        const pollingFullUrl = `${origin}${pollingPath}`;
        this.userStoreItem = {
            ...this.userStoreItem,
            ...{
                url: {
                    origin,
                    request: {
                        fullUrl: requestFullUrl,
                        pathname,
                        search,
                    },
                    polling: {
                        fullUrl: pollingFullUrl,
                        pathname: pollingPath,
                    },
                },
            },
        };
        return this;
    };
    addAsyncUrls = (url) => {
        if (url) {
            this.userStoreItem = {
                ...this.userStoreItem,
                ...{
                    url,
                },
            };
        }
        return this;
    };
    addReportUrls = (req) => {
        const origin = req.get('host');
        const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
        const urlData = parseUrl(req);
        this.userStoreItem = {
            ...this.userStoreItem,
            ...{
                url: {
                    origin: origin || this.userStoreItem.url?.origin || '',
                    ...(this.userStoreItem.url?.request && { request: this.userStoreItem.url.request }),
                    ...(this.userStoreItem.url?.polling && { polling: this.userStoreItem.url.polling }),
                    report: {
                        ...(this.userStoreItem.url?.report && this.userStoreItem.url.report),
                        fullUrl,
                        ...(urlData && urlData.search && { search: urlData.search }),
                    },
                },
            },
        };
        return this;
    };
    addLoadType = (loadType) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            loadType,
        };
        return this;
    };
    addQuery = (queryData) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            ...(queryData && {
                query: {
                    data: queryData.query,
                    summary: queryData.querySummary,
                },
            }),
        };
        return this;
    };
    addInteractiveQuery = (queryData) => {
        if (queryData) {
            this.userStoreItem = {
                ...this.userStoreItem,
                ...{
                    interactiveQuery: {
                        data: queryData.query,
                        summary: queryData.querySummary,
                    },
                },
            };
        }
        return this;
    };
    addStatus = (status) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            status,
        };
        return this;
    };
    addMetrics = (metrics) => {
        this.userStoreItem = {
            ...this.userStoreItem,
            metrics: metrics.filter((metric) => metric.display).map((metric) => ({ name: metric.display || '' })),
        };
        return this;
    };
    addTimestamp = () => {
        const { status } = this.userStoreItem;
        const ts = new Date();
        switch (status) {
            case UserReports.RequestStatus.FAILED:
                this.userStoreItem.timestamp.failed = ts;
                break;
            case UserReports.RequestStatus.EXPIRED:
                this.userStoreItem.timestamp.expired = ts;
                break;
            case UserReports.RequestStatus.ABORTED:
                this.userStoreItem.timestamp.aborted = ts;
                break;
            case UserReports.RequestStatus.FINISHED:
                this.userStoreItem.timestamp.completed = ts;
                break;
            case UserReports.RequestStatus.SUBMITTED:
                this.userStoreItem.timestamp.requested = ts;
                break;
            case UserReports.RequestStatus.STARTED:
            case UserReports.RequestStatus.PICKED:
                break;
            default:
                this.userStoreItem.timestamp.lastViewed = ts;
                break;
        }
        return this;
    };
    addDefinitionsPath = (definitionsPath, dpdPathFromQuery) => {
        if (definitionsPath) {
            this.userStoreItem = {
                ...this.userStoreItem,
                dataProductDefinitionsPath: definitionsPath,
                dpdPathFromQuery,
            };
        }
        return this;
    };
}

exports.UserStoreItemBuilder = UserStoreItemBuilder;
exports.default = UserStoreItemBuilder;
//# sourceMappingURL=UserStoreItemBuilder.js.map
