"use strict";
/* v8 ignore start */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SerialPort = void 0;
const node_os_1 = require("node:os");
const bindings_cpp_1 = require("@serialport/bindings-cpp");
// This file was copied from https://github.com/serialport/node-serialport/blob/master/packages/serialport/lib/serialport.ts.
const stream_1 = require("@serialport/stream");
const DetectedBinding = (0, bindings_cpp_1.autoDetect)();
class SerialPort extends stream_1.SerialPortStream {
    static list = DetectedBinding.list;
    static binding = DetectedBinding;
    constructor(options, openCallback) {
        const opts = {
            binding: DetectedBinding,
            ...options,
        };
        if ((0, node_os_1.platform)() === "win32") {
            // this controls `DTR` on "open", whereas on Unix, it's on "close"
            // https://github.com/serialport/bindings-cpp/blob/19820c39fbbedc1b5f09d6508b5ef1268df3d455/src/serialport_win.cpp#L123-L127
            // https://github.com/serialport/bindings-cpp/blob/19820c39fbbedc1b5f09d6508b5ef1268df3d455/src/serialport_unix.cpp#L254-L256
            opts.hupcl = false;
        }
        super(opts, openCallback);
    }
    async asyncOpen() {
        return await new Promise((resolve, reject) => {
            this.open((err) => (err ? reject(err) : resolve()));
        });
    }
    async asyncClose() {
        return await new Promise((resolve, reject) => {
            this.close((err) => (err ? reject(err) : resolve()));
        });
    }
    async asyncFlush() {
        return await new Promise((resolve, reject) => {
            this.flush((err) => (err ? reject(err) : resolve()));
        });
    }
    async asyncFlushAndClose() {
        await this.asyncFlush();
        await this.asyncClose();
    }
    async asyncGet() {
        return await new Promise((resolve, reject) => {
            // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
            this.get((err, options) => (err ? reject(err) : resolve(options)));
        });
    }
    async asyncSet(options) {
        return await new Promise((resolve, reject) => {
            this.set(options, (err) => (err ? reject(err) : resolve()));
        });
    }
}
exports.SerialPort = SerialPort;
//# sourceMappingURL=serialPort.js.map