"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VectorConversionError = void 0;
exports.fileExists = fileExists;
exports.isFloat32ArrayInstance = isFloat32ArrayInstance;
exports.ensureAbsolutePath = ensureAbsolutePath;
exports.convertToFloat32Array = convertToFloat32Array;
exports.writeFileHeader = writeFileHeader;
const fs = __importStar(require("fs"));
const fsPromises = __importStar(require("fs/promises"));
const path_1 = __importDefault(require("path"));
function fileExists(filePath) {
    return fs.existsSync(filePath);
}
function isFloat32ArrayInstance(arr) {
    return arr instanceof Float32Array;
}
function ensureAbsolutePath(filePath) {
    if (!path_1.default.isAbsolute(filePath)) {
        // Convert relative path to absolute using current working directory
        return path_1.default.resolve(process.cwd(), filePath);
    }
    return filePath;
}
function convertToFloat32Array(arr, index, metadata) {
    try {
        // If it's already a Float32Array, return it
        if (arr instanceof Float32Array) {
            return arr;
        }
        // If it's another TypedArray, we can convert it directly
        if (ArrayBuffer.isView(arr)) {
            return new Float32Array(arr);
        }
        // If it's a regular array, convert it
        if (Array.isArray(arr)) {
            return new Float32Array(arr);
        }
        throw new Error("Unsupported array type");
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        throw new VectorConversionError(`Vector conversion failed${index !== undefined ? ` at index ${index}` : ""}: ${errorMessage}`, index, metadata);
    }
}
class VectorConversionError extends Error {
    constructor(message, index, metadata) {
        super(message);
        this.index = index;
        this.metadata = metadata;
        this.name = "VectorConversionError";
    }
}
exports.VectorConversionError = VectorConversionError;
async function writeFileHeader(tmpPath, vectors, dims) {
    try {
        const buffer = Buffer.alloc(8);
        buffer.writeInt32LE(vectors, 0);
        buffer.writeInt32LE(dims, 4);
        // Open with 'wx' flag - creates a new file and fails if it exists
        const fileHandle = await fsPromises.open(tmpPath, "wx");
        await fileHandle.write(buffer);
        // Force flush to disk
        await fileHandle.sync();
        await fileHandle.close();
    }
    catch (error) {
        throw error;
    }
}
//# sourceMappingURL=utils.js.map