"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilterPattern = exports.termToInt = exports.uniqueQuads = exports.parseQuads = exports.serializeQuads = void 0;
const arrayify_stream_1 = __importDefault(require("arrayify-stream"));
const n3_1 = require("n3");
const StreamUtil_1 = require("./StreamUtil");
const TermUtil_1 = require("./TermUtil");
/**
 * Helper function for serializing an array of quads, with as result a Readable object.
 * @param quads - The array of quads.
 * @param contentType - The content-type to serialize to.
 *
 * @returns The Readable object.
 */
function serializeQuads(quads, contentType) {
    return (0, StreamUtil_1.pipeSafely)((0, StreamUtil_1.guardedStreamFrom)(quads), new n3_1.StreamWriter({ format: contentType }));
}
exports.serializeQuads = serializeQuads;
/**
 * Helper function to convert a Readable into an array of quads.
 * @param readable - The readable object.
 * @param options - Options for the parser.
 *
 * @returns A promise containing the array of quads.
 */
async function parseQuads(readable, options = {}) {
    return (0, arrayify_stream_1.default)((0, StreamUtil_1.pipeSafely)(readable, new n3_1.StreamParser(options)));
}
exports.parseQuads = parseQuads;
/**
 * Filter out duplicate quads from an array.
 * @param quads - Quads to filter.
 *
 * @returns A new array containing the unique quads.
 */
function uniqueQuads(quads) {
    const uniques = [];
    for (const quad of quads) {
        if (!uniques.some((item) => quad.equals(item))) {
            uniques.push(quad);
        }
    }
    return uniques;
}
exports.uniqueQuads = uniqueQuads;
/**
 * Converts a term to a number. Returns undefined if the term was undefined.
 *
 * @param term - Term to parse.
 * @param radix - Radix to use when parsing. Default is 10.
 */
function termToInt(term, radix = 10) {
    if (term) {
        return Number.parseInt(term.value, radix);
    }
}
exports.termToInt = termToInt;
/**
 * Represents a triple pattern to be used as a filter.
 */
class FilterPattern {
    subject;
    predicate;
    object;
    /**
     * @param subject - Optionally filter based on a specific subject.
     * @param predicate - Optionally filter based on a predicate.
     * @param object - Optionally filter based on a specific object.
     */
    constructor(subject, predicate, object) {
        this.subject = typeof subject === 'string' ? (0, TermUtil_1.toNamedTerm)(subject) : null;
        this.predicate = typeof predicate === 'string' ? (0, TermUtil_1.toNamedTerm)(predicate) : null;
        this.object = typeof object === 'string' ? (0, TermUtil_1.toNamedTerm)(object) : null;
    }
}
exports.FilterPattern = FilterPattern;
//# sourceMappingURL=QuadUtil.js.map