"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalUploadSource = void 0;
const urijs_1 = __importDefault(require("urijs"));
class LocalUploadSource {
    constructor(path, name = "") {
        this.ensureNonBrowserEnvironment();
        this._uri = new urijs_1.default(path);
        if (this._uri.protocol() !== "file") {
            throw new Error("Invalid protocol. Only paths using the file protocol are supported.");
        }
        this.name = name.length > 0 ? name : this._uri.filename();
    }
    getReadableSourceAsync() {
        return __awaiter(this, void 0, void 0, function* () {
            this.ensureNonBrowserEnvironment();
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const fs = require("fs");
            const buffer = fs.readFileSync(this._uri.path());
            return Promise.resolve(buffer);
        });
    }
    // https://github.com/axios/axios/blob/16aa2ce7fa42e7c46407b78966b7521d8e588a72/lib/utils.js#L208-L218
    isStandardBrowserEnv() {
        if (typeof navigator !== "undefined" &&
            // eslint-disable-next-line deprecation/deprecation
            (navigator.product === "ReactNative" || navigator.product === "NativeScript" || navigator.product === "NS")) {
            return false;
        }
        return typeof window !== "undefined" && typeof document !== "undefined";
    }
    ensureNonBrowserEnvironment() {
        if (this.isStandardBrowserEnv()) {
            throw Error("LocalUploadSource is not supported when running in a browser environment.");
        }
    }
}
exports.LocalUploadSource = LocalUploadSource;
//# sourceMappingURL=local-upload-source.js.map