import { AriaNativescriptCommon } from './common';
import * as application from '@nativescript/core/application';
export class AriaNativescript extends AriaNativescriptCommon {
    constructor() {
        super(...arguments);
        this.esdcInstance = null;
    }
    async initEsdc(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.plugin.sdc.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.plugin.sdc.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.plugin.sdc.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.plugin.sdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getEnvironmentParameters(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getEnvironmentParameters(new me.ariapos.plugin.sdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getStatus(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getStatus(new me.ariapos.plugin.sdc.SdcResultCallback({
                onResult(result) {
                    resolve(JSON.parse(result));
                },
            }));
        });
    }
    getAttention(options) {
        const sdcInstance = this.getSdcInstance(options);
        return new Promise((resolve, reject) => {
            sdcInstance.getAttention(new me.ariapos.plugin.sdc.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.plugin.sdc.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.plugin.sdc.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.plugin.sdc.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.plugin.sdc.Esdc(context, activity, url, true);
        }
        else if (sdc === 'VSDC') {
            return new me.ariapos.plugin.sdc.Vsdc(context, activity, url, pac, certAlias);
        }
        else if (sdc === 'DEVSDC') {
            return new me.ariapos.plugin.sdc.DevSdc(context, activity, url);
        }
        return null;
    }
    /**
     * Print Text & QRCode on printer
     * @param Printer printer
     * @param PrintData data
     */
    print(printer, data) {
        const ariaPrint = new me.ariapos.plugin.print.AriaPrint(application.android.context, printer.width, printer.charsPerLine, printer.dpi, printer.ipAddress, printer.qrCodeSize);
        ariaPrint.print(data.text, data.qrCode, !!data.qrCodeEmbed);
    }
    /**
     * Feed Paper
     * @param Printer printer
     */
    feedPaper(printer) {
        const ariaPrint = new me.ariapos.plugin.print.AriaPrint(application.android.context, printer.width, printer.charsPerLine, printer.dpi, printer.ipAddress, printer.qrCodeSize);
        ariaPrint.feedPaper();
    }
}
//# sourceMappingURL=index.android.js.map