import { NativescriptEsdcCommon } from './common';
import * as application from '@nativescript/core/application';
export class NativescriptEsdc extends NativescriptEsdcCommon {
    constructor() {
        super(...arguments);
        this.esdcInstance = null;
    }
    async init(options = {}) {
        this.esdcInstance = this.getSdcInstance({ sdc: 'ESDC', url: options.url });
    }
    chooseCertificate(options) {
        if (options.sdc !== 'VSDC') {
            return Promise.resolve({ error: { code: 'not_supported' } });
        }
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.chooseCertificate(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    verifyPin(pin, options) {
        if (options.sdc === 'VSDC') {
            return Promise.resolve({ error: { code: 'not_supported' } });
        }
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.verifyPin(pin, new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    createInvoice(invoice, options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.createInvoice(JSON.stringify(invoice), new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getInvoice(requestId, options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getInvoice(requestId, new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getEnvironmentParameters(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getEnvironmentParameters(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getStatus(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getStatus(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getAttention(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getAttention(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getUiData(options) {
        if (options.sdc !== 'ESDC') {
            return Promise.resolve({ error: { code: 'not_supported' } });
        }
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getUiData(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    storeLocalAuditFiles(options) {
        if (options.sdc !== 'ESDC') {
            return Promise.resolve({ error: { code: 'not_supported' } });
        }
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.storeLocalAuditFiles(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    executeCommandsFromStorage(options) {
        if (options.sdc !== 'ESDC') {
            return Promise.resolve({ error: { code: 'not_supported' } });
        }
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.executeCommandsFromStorage(new me.ariapos.esdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getSdcInstance(options) {
        const { url, pac, certAlias, sdc } = options;
        const context = application.android.context;
        const activity = application.android.foregroundActivity || application.android.startActivity;
        if (sdc === 'ESDC') {
            if (this.esdcInstance)
                return this.esdcInstance;
            return new me.ariapos.esdc.Esdc(context, activity, url, true);
        }
        else if (sdc === 'VSDC')
            return new me.ariapos.esdc.Vsdc(context, activity, url, pac, certAlias);
        else if (sdc === 'DEVSDC')
            return new me.ariapos.esdc.DevSdc(context, activity, url);
        return null;
    }
}
//# sourceMappingURL=index.android.js.map