"use strict";
/**
 * This file was auto-generated by Fern from our API Definition.
 */
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __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.File_ = void 0;
const core = __importStar(require("../../../../core"));
const openai_1 = require("../../../..");
const environments = __importStar(require("../../../../environments"));
const url_join_1 = __importDefault(require("url-join"));
const serializers = __importStar(require("../../../../serialization"));
const errors = __importStar(require("../../../../errors"));
const form_data_1 = __importDefault(require("form-data"));
class File_ {
    options;
    constructor(options) {
        this.options = options;
    }
    /**
     * Returns a list of files that belong to the user's organization.
     * @throws {OpenAI.UnauthorizedError}
     * @throws {OpenAI.RateLimitError}
     * @throws {OpenAI.InternalServerError}
     */
    async list() {
        const _response = await (this.options.fetcher ?? core.fetcher)({
            url: (0, url_join_1.default)(environments.OpenAIEnvironment.Production, "/files"),
            method: "GET",
            headers: {
                Authorization: await this._getAuthorizationHeader(),
                "OpenAI-Organization": await core.Supplier.get(this.options.organization),
            },
            contentType: "application/json",
        });
        if (_response.ok) {
            return await serializers.ListFilesResponse.parseOrThrow(_response.body, {
                unrecognizedObjectKeys: "passthrough",
                allowUnrecognizedUnionMembers: true,
                allowUnrecognizedEnumValues: true,
            });
        }
        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new openai_1.OpenAI.UnauthorizedError();
                case 429:
                    throw new openai_1.OpenAI.RateLimitError();
                case 500:
                    throw new openai_1.OpenAI.InternalServerError();
                default:
                    throw new errors.OpenAIError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                    });
            }
        }
        switch (_response.error.reason) {
            case "non-json":
                throw new errors.OpenAIError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.rawBody,
                });
            case "timeout":
                throw new errors.OpenAITimeoutError();
            case "unknown":
                throw new errors.OpenAIError({
                    message: _response.error.errorMessage,
                });
        }
    }
    /**
     * Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
     *
     * @throws {OpenAI.UnauthorizedError}
     * @throws {OpenAI.RateLimitError}
     * @throws {OpenAI.InternalServerError}
     */
    async upload(file, request) {
        const _request = new form_data_1.default();
        _request.append("file", file);
        _request.append("purpose", request.purpose);
        const _response = await (this.options.fetcher ?? core.fetcher)({
            url: (0, url_join_1.default)(environments.OpenAIEnvironment.Production, "/files"),
            method: "POST",
            headers: {
                Authorization: await this._getAuthorizationHeader(),
                "OpenAI-Organization": await core.Supplier.get(this.options.organization),
            },
            contentType: "multipart/form-data",
            body: _request,
        });
        if (_response.ok) {
            return await serializers.OpenAiFile.parseOrThrow(_response.body, {
                unrecognizedObjectKeys: "passthrough",
                allowUnrecognizedUnionMembers: true,
                allowUnrecognizedEnumValues: true,
            });
        }
        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new openai_1.OpenAI.UnauthorizedError();
                case 429:
                    throw new openai_1.OpenAI.RateLimitError();
                case 500:
                    throw new openai_1.OpenAI.InternalServerError();
                default:
                    throw new errors.OpenAIError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                    });
            }
        }
        switch (_response.error.reason) {
            case "non-json":
                throw new errors.OpenAIError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.rawBody,
                });
            case "timeout":
                throw new errors.OpenAITimeoutError();
            case "unknown":
                throw new errors.OpenAIError({
                    message: _response.error.errorMessage,
                });
        }
    }
    /**
     * Returns information about a specific file.
     * @throws {OpenAI.UnauthorizedError}
     * @throws {OpenAI.RateLimitError}
     * @throws {OpenAI.InternalServerError}
     */
    async retrieve(fileId) {
        const _response = await (this.options.fetcher ?? core.fetcher)({
            url: (0, url_join_1.default)(environments.OpenAIEnvironment.Production, `/files/${await serializers.FileId.jsonOrThrow(fileId)}`),
            method: "GET",
            headers: {
                Authorization: await this._getAuthorizationHeader(),
                "OpenAI-Organization": await core.Supplier.get(this.options.organization),
            },
            contentType: "application/json",
        });
        if (_response.ok) {
            return await serializers.OpenAiFile.parseOrThrow(_response.body, {
                unrecognizedObjectKeys: "passthrough",
                allowUnrecognizedUnionMembers: true,
                allowUnrecognizedEnumValues: true,
            });
        }
        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new openai_1.OpenAI.UnauthorizedError();
                case 429:
                    throw new openai_1.OpenAI.RateLimitError();
                case 500:
                    throw new openai_1.OpenAI.InternalServerError();
                default:
                    throw new errors.OpenAIError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                    });
            }
        }
        switch (_response.error.reason) {
            case "non-json":
                throw new errors.OpenAIError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.rawBody,
                });
            case "timeout":
                throw new errors.OpenAITimeoutError();
            case "unknown":
                throw new errors.OpenAIError({
                    message: _response.error.errorMessage,
                });
        }
    }
    /**
     * Delete a file.
     * @throws {OpenAI.UnauthorizedError}
     * @throws {OpenAI.RateLimitError}
     * @throws {OpenAI.InternalServerError}
     */
    async delete(fileId) {
        const _response = await (this.options.fetcher ?? core.fetcher)({
            url: (0, url_join_1.default)(environments.OpenAIEnvironment.Production, `/files/${await serializers.FileId.jsonOrThrow(fileId)}`),
            method: "DELETE",
            headers: {
                Authorization: await this._getAuthorizationHeader(),
                "OpenAI-Organization": await core.Supplier.get(this.options.organization),
            },
            contentType: "application/json",
        });
        if (_response.ok) {
            return await serializers.DeleteFileResponse.parseOrThrow(_response.body, {
                unrecognizedObjectKeys: "passthrough",
                allowUnrecognizedUnionMembers: true,
                allowUnrecognizedEnumValues: true,
            });
        }
        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new openai_1.OpenAI.UnauthorizedError();
                case 429:
                    throw new openai_1.OpenAI.RateLimitError();
                case 500:
                    throw new openai_1.OpenAI.InternalServerError();
                default:
                    throw new errors.OpenAIError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                    });
            }
        }
        switch (_response.error.reason) {
            case "non-json":
                throw new errors.OpenAIError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.rawBody,
                });
            case "timeout":
                throw new errors.OpenAITimeoutError();
            case "unknown":
                throw new errors.OpenAIError({
                    message: _response.error.errorMessage,
                });
        }
    }
    /**
     * Returns the contents of the specified file
     * @throws {OpenAI.UnauthorizedError}
     * @throws {OpenAI.RateLimitError}
     * @throws {OpenAI.InternalServerError}
     */
    async download(fileId) {
        const _response = await (this.options.fetcher ?? core.fetcher)({
            url: (0, url_join_1.default)(environments.OpenAIEnvironment.Production, `/files/${await serializers.FileId.jsonOrThrow(fileId)}/content`),
            method: "GET",
            headers: {
                Authorization: await this._getAuthorizationHeader(),
                "OpenAI-Organization": await core.Supplier.get(this.options.organization),
            },
            contentType: "application/json",
        });
        if (_response.ok) {
            return await serializers.file.download.Response.parseOrThrow(_response.body, {
                unrecognizedObjectKeys: "passthrough",
                allowUnrecognizedUnionMembers: true,
                allowUnrecognizedEnumValues: true,
            });
        }
        if (_response.error.reason === "status-code") {
            switch (_response.error.statusCode) {
                case 401:
                    throw new openai_1.OpenAI.UnauthorizedError();
                case 429:
                    throw new openai_1.OpenAI.RateLimitError();
                case 500:
                    throw new openai_1.OpenAI.InternalServerError();
                default:
                    throw new errors.OpenAIError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                    });
            }
        }
        switch (_response.error.reason) {
            case "non-json":
                throw new errors.OpenAIError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.rawBody,
                });
            case "timeout":
                throw new errors.OpenAITimeoutError();
            case "unknown":
                throw new errors.OpenAIError({
                    message: _response.error.errorMessage,
                });
        }
    }
    async _getAuthorizationHeader() {
        const bearer = await core.Supplier.get(this.options.token);
        if (bearer != null) {
            return `Bearer ${bearer}`;
        }
        return undefined;
    }
}
exports.File_ = File_;
