"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const EndUserLibraryLoader = require('../eusw').EndUserLibraryLoader;
const EUSignCP = require('../eusw').EUSignCP;
class EUSignCPCore {
    // eslint-disable-next-line no-empty-function,no-useless-constructor,no-unused-vars
    constructor(props) {
        this.props = props;
        this.m_CAs = [];
    }
    loadLib() {
        return new Promise((resolve, reject) => {
            // @ts-ignore
            this.libLoader = new EndUserLibraryLoader(EndUserLibraryLoader.LIBRARY_TYPE_DEFAULT, 'euSign', EndUserLibraryLoader.EU_DEFAULT_LANG);
            // @ts-ignore
            this.libLoader.onload = (library) => {
                this.euSign = library;
                resolve();
            };
            this.libLoader.onerror = (error) => {
                reject(error);
            };
            this.libLoader.load();
        });
    }
    initLib() {
        return new Promise((resolve1, reject1) => {
            new Promise((resolve, reject) => {
                // @ts-ignore
                this.euSign.SetRuntimeParameter(this.euSign.EU_SAVE_SETTINGS_PARAMETER, this.euSign.EU_SETTINGS_ID_PROXY, resolve, reject);
            })
                .then(() => this.SetUIMode(false))
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                this.euSign.Initialize(resolve, reject);
            }))
                .then(() => this.SetUIMode(false))
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                this.euSign.SetCharset('UTF-8', resolve, reject);
            }))
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                this.euSign.InitializeMandatorySettings(resolve, reject);
            }))
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                this.euSign.SetRuntimeParameter(this.euSign.EU_SIGN_TYPE_PARAMETER, this.euSign.EU_SIGN_TYPE_CADES_X_LONG |
                    this.euSign.EU_SIGN_TYPE_CADES_X_LONG_TRUSTED, resolve, reject);
            }))
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                const settings1 = this.euSign.CreateFileStoreSettings();
                settings1.SetPath('');
                settings1.SetSaveLoadedCerts(true);
                // @ts-ignore
                return this.euSign.SetFileStoreSettings(settings1, resolve, reject);
            }))
                .then(() => this.loadCAServers())
                .then(() => new Promise((resolve, reject) => {
                // @ts-ignore
                const settings = this.euSign.CreateOCSPAccessInfoModeSettings();
                settings.SetEnabled(true);
                // @ts-ignore
                this.euSign.SetOCSPAccessInfoModeSettings(settings, resolve, reject);
            }))
                .then(() => new Promise((resolve, reject) => {
                const ocspAccessPointArray = [];
                for (let e = this.m_CAs, i = 0; i < e.length; i++) {
                    // @ts-ignore
                    const settings = this.euSign.CreateOCSPAccessInfoSettings();
                    // @ts-ignore
                    settings.SetAddress(e[i].ocspAccessPointAddress);
                    // @ts-ignore
                    settings.SetPort(e[i].ocspAccessPointPort);
                    // @ts-ignore
                    settings.SetIssuerCN(e[i].issuerCNs[0]);
                    ocspAccessPointArray.push(settings);
                }
                // console.log(ocspAccessPointArray);
                // @ts-ignore
                this.euSign.SetOCSPAccessInfoSettings(ocspAccessPointArray, resolve, reject);
            }))
                .then(() => {
                // @ts-ignore
                const settings = this.euSign.CreateModeSettings();
                settings.SetOfflineMode(false);
                return this.SetModeSettings(settings);
            })
                .then(() => this.loadCACert())
                .then((CACert) => this.SaveCertificates(CACert))
                .then(() => resolve1())
                .catch((error) => reject1(error));
        });
    }
    loadCAServers() {
        return new Promise((resolve, reject) => {
            const storage = sessionStorage.getItem('CAServers');
            const storageName = sessionStorage.getItem('CAServersName');
            if (storage && storageName && this.props.CAServers === storageName) {
                this.m_CAs = JSON.parse(storage);
                resolve();
                return;
            }
            const xhr = new XMLHttpRequest();
            const isReady = 4;
            const statusOk = 200;
            xhr.onload = () => {
                if (xhr.readyState === isReady) {
                    if (xhr.status === statusOk) {
                        this.m_CAs = JSON.parse(xhr.responseText);
                        sessionStorage.setItem('CAServers', xhr.responseText);
                        sessionStorage.setItem('CAServersName', this.props.CAServers);
                        resolve();
                    }
                    else {
                        reject('Помилка при завантаженні конфігурації серверів');
                    }
                }
            };
            xhr.onerror = () => reject('Помилка при завантаженні конфігурації серверів');
            xhr.open('GET', this.props.CAServers, true);
            xhr.send();
        });
    }
    loadCACert() {
        return new Promise((resolve, reject) => {
            const storage = sessionStorage.getItem('CACertificates');
            const storageName = sessionStorage.getItem('CACertificatesName');
            if (storage && storageName && this.props.CACert === storageName) {
                resolve(this.BASE64Decode(storage));
                return;
            }
            const xhr = new XMLHttpRequest();
            const isReady = 4;
            const statusOk = 200;
            xhr.onload = () => {
                if (xhr.readyState === isReady) {
                    if (xhr.status === statusOk) {
                        const result = new Uint8Array(xhr.response);
                        this.BASE64Encode(result).then((res) => {
                            sessionStorage.setItem('CACertificates', res);
                            sessionStorage.setItem('CACertificatesName', this.props.CACert);
                        });
                        resolve(result);
                    }
                    else {
                        reject('Помилка при завантаженні сертифікатів');
                    }
                }
            };
            xhr.onerror = () => reject('Помилка при завантаженні сертифікатів');
            xhr.open('GET', this.props.CACert, true);
            xhr.responseType = 'arraybuffer';
            xhr.send();
        });
    }
    BASE64Encode(data) {
        return new Promise((resolve, reject) => {
            // @ts-ignore
            this.euSign.BASE64Encode(data, resolve, reject);
        });
    }
    BASE64Decode(data) {
        return new Promise((resolve, reject) => {
            // @ts-ignore
            this.euSign.BASE64Decode(data, resolve, reject);
        });
    }
    SetUIMode(uiMode) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SetUIMode(uiMode, resolve, reject));
    }
    GetFileStoreSettings() {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetFileStoreSettings(resolve, reject));
    }
    ResetPrivateKey() {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.ResetPrivateKey(resolve, reject));
    }
    SetModeSettings(settings) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SetModeSettings(settings, resolve, reject));
    }
    SaveCertificate(certificate) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SaveCertificate(certificate, resolve, reject));
    }
    SaveCertificates(certificates) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SaveCertificates(certificates, resolve, reject));
    }
    GetKeyMediaTypes() {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetKeyMediaTypes(resolve, reject));
    }
    GetKeyMediaDevices(typeIndex) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetKeyMediaDevices(typeIndex, resolve, reject));
    }
    GetKeyInfo(keyMedia) {
        return new Promise((resolve, reject) => this.euSign.GetKeyInfo(keyMedia, resolve, reject));
    }
    GetKeyInfoBinary(privateKey, password) {
        return new Promise((resolve, reject) => this.euSign.GetKeyInfoBinary(privateKey, password, resolve, reject));
    }
    CtxReadPrivateKeyBinary(privateKey, password) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.ReadPrivateKeyBinary(privateKey, password, resolve, reject));
    }
    CtxReadPrivateKey(keyMedia) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.ReadPrivateKeySilently(keyMedia.typeIndex, keyMedia.devIndex, keyMedia.password, resolve, reject));
    }
    SetTSPSettings(settings) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SetTSPSettings(settings, resolve, reject));
    }
    SetOCSPSettings(settings) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SetOCSPSettings(settings, resolve, reject));
    }
    SetCMPSettings(settings) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SetCMPSettings(settings, resolve, reject));
    }
    CreateKeyMedia() {
        // @ts-ignore
        return this.euSign.CreateKeyMedia();
    }
    GetCertificatesByKeyInfo(keyInfo, cmpServers, cmpServersPorts) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetCertificatesByKeyInfo(keyInfo, cmpServers, cmpServersPorts, resolve, reject));
    }
    GetPrivateKeyOwnerInfo() {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetPrivateKeyOwnerInfo(resolve, reject));
    }
    EnumOwnCertificates(index) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.EnumOwnCertificates(index, resolve, reject));
    }
    GetCertificate(issuer, serial) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.GetCertificate(issuer, serial, resolve, reject));
    }
    SignData(data, appendCert, externalSign) {
        if (externalSign) {
            // @ts-ignore
            return new Promise((resolve, reject) => this.euSign.Sign(data, resolve, reject));
        }
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SignInternal(appendCert, data, resolve, reject));
    }
    SignRSA(data, appendCert, externalSign) {
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.SignRSA(data, appendCert, externalSign, resolve, reject));
    }
    CreateEmptySign(data) {
        if (data) {
            // @ts-ignore
            return new Promise((resolve, reject) => this.euSign.CreateEmptySign(data, resolve, reject));
        }
        // @ts-ignore
        return new Promise((resolve, reject) => this.euSign.CreateEmptySign(resolve, reject));
    }
    AppendSign(data, signature) {
        return new Promise((resolve, reject) => this.euSign.AppendSign(data, signature, resolve, reject));
    }
    GetSignsCount(signature) {
        return new Promise((resolve, reject) => this.euSign.GetSignsCount(signature, resolve, reject));
    }
    // GetSignType(index:number, signature: any): Promise<EndUserCertificateInfoEx> {
    //     return new Promise((resolve, reject) => this.euSign.GetSignType(index, signature, resolve, reject))
    // }
    // GetSignTimeInfo(index:number, signature: any): Promise<EndUserTimeInfo> {
    //     return new Promise((resolve, reject) => this.euSign.GetSignTimeInfo(index, signature, resolve, reject))
    // }
    //
    // GetSignerInfo(index:number, signature: any): Promise<EndUserCertificateInfoEx> {
    //     return new Promise((resolve, reject) => this.euSign.GetSignerInfo(index, signature, resolve, reject))
    // }
    GetSignType(index, signature) {
        return this.euSign.GetSignType(index, signature);
    }
    GetSignTimeInfo(index, signature) {
        return this.euSign.GetSignTimeInfo(index, signature);
    }
    GetSignerInfo(index, signature) {
        return this.euSign.GetSignerInfo(index, signature);
    }
}
exports.default = EUSignCPCore;
