import { Kind as AuthKind } from '../RequestAuthorization.js';
export class Normalizer {
    static normalizeRequest(request) {
        if (!request) {
            return undefined;
        }
        const typed = { ...request };
        if (typed.legacyProject) {
            const saved = typed;
            if (!saved.projects) {
                saved.projects = [];
            }
            saved.projects[saved.projects.length] = typed.legacyProject;
            delete typed.legacyProject;
        }
        const skipKeys = ['_id', '_rev', '_deleted'];
        Object.keys(typed).forEach((key) => {
            if (key[0] === '_' && skipKeys.indexOf(key) === -1) {
                // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
                delete typed[key];
            }
        });
        if (!typed.created) {
            typed.created = Date.now();
        }
        if (!typed.updated) {
            typed.updated = typed.created;
        }
        if (!typed.midnight) {
            const day = new Date(typed.updated);
            day.setHours(0, 0, 0, 0);
            typed.midnight = day.getTime();
        }
        if (!typed.type) {
            if (typed.name) {
                typed.type = 'saved';
            }
            else {
                typed.type = 'history';
            }
        }
        else if (typed.type === 'drive' || typed.type === 'google-drive') {
            typed.type = 'saved';
        }
        if (typed.type === 'history' && !typed._id) {
            typed._id = Normalizer.generateHistoryId(typed);
        }
        return Normalizer.normalizeAuthorization(typed);
    }
    static generateHistoryId(request) {
        const { method, url } = request;
        const d = new Date();
        d.setHours(0, 0, 0, 0);
        const time = d.getTime();
        const encUrl = encodeURIComponent(url);
        return `${time}/${encUrl}/${method}`;
    }
    static normalizeAuthorization(request) {
        const typed = request;
        const { auth = {}, authType } = typed;
        if (!authType) {
            return request;
        }
        const requestAuth = {
            config: auth,
            enabled: true,
            type: authType,
            valid: true,
            kind: AuthKind,
        };
        const copy = { ...typed };
        copy.authorization = [requestAuth];
        delete copy.auth;
        delete copy.authType;
        return copy;
    }
    /**
     * Transforms the legacy `TransformedPayload` object to its original data type.
     */
    static restoreTransformedPayload(body) {
        if (!body) {
            return body;
        }
        if (typeof body === 'string') {
            return body;
        }
        if (body instanceof ArrayBuffer) {
            return body;
        }
        const typed = body;
        if (typed.type === 'ArrayBuffer' || typed.type === 'Buffer') {
            const { buffer } = new Uint8Array(typed.data);
            return buffer;
        }
        return undefined;
    }
}
//# sourceMappingURL=Normalizer.js.map