/**
 * A class that processes ARC data to create a standard export object.
 */
export class LegacyExportProcessor {
    electronCookies;
    /**
     * @param electronCookies True if the cookies were read from electron storage
     */
    constructor(electronCookies) {
        this.electronCookies = electronCookies;
    }
    /**
     * Creates an export object for the data.
     *
     * @param exportData
     * @param options Export configuration object
     * @returns ARC export object declaration.
     */
    createExportObject(exportData, options) {
        const result = {
            createdAt: new Date().toISOString(),
            version: options.appVersion,
            kind: options.kind || '',
            electronCookies: this.electronCookies,
        };
        if (options.skipImport) {
            result.loadToWorkspace = true;
        }
        exportData.forEach(({ key, data }) => {
            if (!data) {
                return;
            }
            result[key] = this.prepareItem(key, data);
        });
        return result;
    }
    prepareItem(key, values) {
        switch (key) {
            case 'authdata':
                return this.prepareAuthData(values);
            case 'clientcertificates':
                return this.prepareClientCertData(values);
            case 'cookies':
                return this.prepareCookieData(values);
            case 'history':
                return this.prepareHistoryDataList(values);
            case 'hostrules':
                return this.prepareHostRulesData(values);
            case 'projects':
                return this.prepareProjectsList(values);
            case 'requests':
                return this.prepareRequestsList(values);
            case 'urlhistory':
                return this.prepareUrlHistoryData(values);
            case 'variables':
                return this.prepareVariablesData(values);
            case 'websocketurlhistory':
                return this.prepareWsUrlHistoryData(values);
            default:
                return undefined;
        }
    }
    /**
     * Maps list of request to the export object.
     * @param requests The list of requests to process.
     */
    prepareRequestsList(requests) {
        const result = requests.map((item) => {
            const request = { ...item };
            // @ts-expect-error This library will likely go away
            if (item.legacyProject) {
                if (item.projects) {
                    // @ts-expect-error This library will likely go away
                    request.projects[item.projects.length] = item.legacyProject;
                }
                else {
                    // @ts-expect-error This library will likely go away
                    request.projects = [request.legacyProject];
                }
                // @ts-expect-error This library will likely go away
                delete request.legacyProject;
            }
            request.kind = 'ARC#HttpRequest';
            request.key = item._id;
            delete request._rev;
            delete request._id;
            return request;
        });
        return result;
    }
    prepareProjectsList(projects) {
        return projects.map((item) => {
            const project = { ...item };
            project.kind = 'ARC#ProjectData';
            project.key = item._id;
            // @ts-expect-error This library will likely go away
            delete project._rev;
            // @ts-expect-error This library will likely go away
            delete project._id;
            return project;
        });
    }
    /**
     * @param history The list of requests to process.
     */
    prepareHistoryDataList(history) {
        const result = history.map((item) => {
            const request = { ...item };
            request.kind = 'ARC#HttpRequest';
            request.key = item._id;
            delete request._rev;
            delete request._id;
            return request;
        });
        return result;
    }
    prepareWsUrlHistoryData(data) {
        const result = data.map((item) => {
            const history = { ...item };
            history.key = item._id;
            delete history._rev;
            delete history._id;
            history.kind = 'ARC#WebsocketHistoryData';
            return history;
        });
        return result;
    }
    prepareUrlHistoryData(data) {
        const result = data.map((item) => {
            const history = { ...item };
            history.key = item._id;
            delete history._rev;
            delete history._id;
            history.kind = 'ARC#UrlHistoryData';
            return history;
        });
        return result;
    }
    prepareVariablesData(data) {
        const result = [];
        data.forEach((item) => {
            const value = { ...item };
            if (!value.environment) {
                // PouchDB creates some views in the main datastore and it is added to
                // get all docs function without any reason. It should be eliminated
                return;
            }
            if (value.variable) {
                value.name = value.variable;
                delete value.variable;
            }
            value.key = item._id;
            delete value._rev;
            delete value._id;
            value.kind = 'ARC#Variable';
            result.push(value);
        });
        return result;
    }
    prepareAuthData(authData) {
        const result = authData.map((item) => {
            const value = item;
            value.key = item._id;
            delete value._rev;
            delete value._id;
            value.kind = 'ARC#AuthData';
            return value;
        });
        return result;
    }
    prepareCookieData(cookies) {
        const isElectron = this.electronCookies;
        const result = cookies.map((item) => {
            const value = item;
            if (!isElectron) {
                value.key = item._id;
                delete value._rev;
                delete value._id;
            }
            value.kind = 'ARC#Cookie';
            return item;
        });
        return result;
    }
    /**
     * @param {ARCHostRule[]} hostRules
     * @return {ExportArcHostRule[]}
     */
    prepareHostRulesData(hostRules) {
        return hostRules.map((item) => {
            const value = item;
            value.key = value._id;
            delete value._rev;
            delete value._id;
            value.kind = 'ARC#HostRule';
            return value;
        });
    }
    prepareClientCertData(items) {
        return items.map(({ item, data }) => {
            const value = item;
            value.key = item._id;
            delete value._rev;
            delete value._id;
            value.kind = 'ARC#ClientCertificate';
            value.cert = data.cert;
            if (data.key) {
                value.pKey = data.key;
            }
            return value;
        });
    }
}
//# sourceMappingURL=LegacyExportProcessor.js.map