import { Nfc } from "./nfc.js";
import { NfcUtils } from "./utils.js";
/**
 * A simplified API for common NFC reading operations
 */
export class SimpleNfc {
    constructor() {
        this.scanCallback = null;
        this.nfc = new Nfc();
    }
    /**
     * Check if NFC is available on this device/browser
     */
    async isAvailable() {
        try {
            const result = await this.nfc.isEnabled();
            return result.enabled;
        }
        catch (e) {
            return false;
        }
    }
    /**
     * Start scanning for NFC tags with a simplified callback
     * The callback will receive text content and content type ('text', 'url', or 'other')
     */
    async startReading(callback) {
        this.scanCallback = callback;
        // Set up the tag detection listener
        await this.nfc.addListener("tagDetected", (tag) => {
            var _a, _b, _c, _d;
            // Try to get URL first
            const url = NfcUtils.getUrlFromTag(tag);
            if (url) {
                (_a = this.scanCallback) === null || _a === void 0 ? void 0 : _a.call(this, url, "url");
                return;
            }
            // Then try to get text
            const text = NfcUtils.getTextFromTag(tag);
            if (text) {
                (_b = this.scanCallback) === null || _b === void 0 ? void 0 : _b.call(this, text, "text");
                return;
            }
            // If we got here, we have other content
            if (tag.messages.length > 0 && tag.messages[0].records.length > 0) {
                const firstRecord = tag.messages[0].records[0];
                (_c = this.scanCallback) === null || _c === void 0 ? void 0 : _c.call(this, firstRecord.payload || "Unknown content", "other");
            }
            else {
                (_d = this.scanCallback) === null || _d === void 0 ? void 0 : _d.call(this, "Empty tag", "other");
            }
        });
        // Start the actual scan
        await this.nfc.startScanSession();
    }
    /**
     * Stop scanning for NFC tags
     */
    async stopReading() {
        this.scanCallback = null;
        await this.nfc.stopScanSession();
        await this.nfc.removeAllListeners();
    }
    /**
     * Write a simple text to an NFC tag
     */
    async writeText(text) {
        const textRecord = NfcUtils.createTextRecord(text);
        await this.nfc.write({
            message: {
                records: [textRecord],
            },
        });
    }
    /**
     * Write a URL to an NFC tag
     */
    async writeUrl(url) {
        const urlRecord = NfcUtils.createUriRecord(url);
        await this.nfc.write({
            message: {
                records: [urlRecord],
            },
        });
    }
    /**
     * Read a simple NFC tag
     */
    read() {
        // Dummy implementation
        return { id: "2", data: "simple data" };
    }
}
