"use strict";
/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */
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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PartitionAcceptEnum = void 0;
exports.generalPartition = generalPartition;
const encodings_js_1 = require("../lib/encodings.js");
const files_js_1 = require("../lib/files.js");
const M = __importStar(require("../lib/matchers.js"));
const primitives_js_1 = require("../lib/primitives.js");
const schemas_js_1 = require("../lib/schemas.js");
const security_js_1 = require("../lib/security.js");
const url_js_1 = require("../lib/url.js");
const errors = __importStar(require("../sdk/models/errors/index.js"));
const operations = __importStar(require("../sdk/models/operations/index.js"));
const async_js_1 = require("../sdk/types/async.js");
const blobs_js_1 = require("../sdk/types/blobs.js");
const streams_js_1 = require("../sdk/types/streams.js");
var PartitionAcceptEnum;
(function (PartitionAcceptEnum) {
    PartitionAcceptEnum["applicationJson"] = "application/json";
    PartitionAcceptEnum["textCsv"] = "text/csv";
})(PartitionAcceptEnum || (exports.PartitionAcceptEnum = PartitionAcceptEnum = {}));
/**
 * Summary
 *
 * @remarks
 * Description
 */
function generalPartition(client, request, options) {
    return new async_js_1.APIPromise($do(client, request, options));
}
async function $do(client, request, options) {
    const parsed = (0, schemas_js_1.safeParse)(request, (value) => operations.PartitionRequest$outboundSchema.parse(value), "Input validation failed");
    if (!parsed.ok) {
        return [parsed, { status: "invalid" }];
    }
    const payload = parsed.value;
    const body = new FormData();
    if ((0, blobs_js_1.isBlobLike)(payload.partition_parameters.files)) {
        (0, encodings_js_1.appendForm)(body, "files", payload.partition_parameters.files);
    }
    else if ((0, streams_js_1.isReadableStream)(payload.partition_parameters.files.content)) {
        const buffer = await (0, files_js_1.readableStreamToArrayBuffer)(payload.partition_parameters.files.content);
        const contentType = (0, files_js_1.getContentTypeFromFileName)(payload.partition_parameters.files.fileName)
            || "application/octet-stream";
        const blob = new Blob([buffer], { type: contentType });
        (0, encodings_js_1.appendForm)(body, "files", blob, payload.partition_parameters.files.fileName);
    }
    else {
        const contentType = (0, files_js_1.getContentTypeFromFileName)(payload.partition_parameters.files.fileName)
            || "application/octet-stream";
        (0, encodings_js_1.appendForm)(body, "files", new Blob([payload.partition_parameters.files.content], {
            type: contentType,
        }), payload.partition_parameters.files.fileName);
    }
    if (payload.partition_parameters.chunking_strategy !== undefined) {
        (0, encodings_js_1.appendForm)(body, "chunking_strategy", payload.partition_parameters.chunking_strategy);
    }
    if (payload.partition_parameters.combine_under_n_chars !== undefined) {
        (0, encodings_js_1.appendForm)(body, "combine_under_n_chars", payload.partition_parameters.combine_under_n_chars);
    }
    if (payload.partition_parameters.content_type !== undefined) {
        (0, encodings_js_1.appendForm)(body, "content_type", payload.partition_parameters.content_type);
    }
    if (payload.partition_parameters.coordinates !== undefined) {
        (0, encodings_js_1.appendForm)(body, "coordinates", payload.partition_parameters.coordinates);
    }
    if (payload.partition_parameters.do_not_break_similarity_on_footer_header
        !== undefined) {
        (0, encodings_js_1.appendForm)(body, "do_not_break_similarity_on_footer_header", payload.partition_parameters.do_not_break_similarity_on_footer_header);
    }
    if (payload.partition_parameters.encoding !== undefined) {
        (0, encodings_js_1.appendForm)(body, "encoding", payload.partition_parameters.encoding);
    }
    if (payload.partition_parameters.extract_image_block_types !== undefined) {
        (0, encodings_js_1.appendForm)(body, "extract_image_block_types", payload.partition_parameters.extract_image_block_types);
    }
    if (payload.partition_parameters.gz_uncompressed_content_type !== undefined) {
        (0, encodings_js_1.appendForm)(body, "gz_uncompressed_content_type", payload.partition_parameters.gz_uncompressed_content_type);
    }
    if (payload.partition_parameters.hi_res_model_name !== undefined) {
        (0, encodings_js_1.appendForm)(body, "hi_res_model_name", payload.partition_parameters.hi_res_model_name);
    }
    if (payload.partition_parameters.include_orig_elements !== undefined) {
        (0, encodings_js_1.appendForm)(body, "include_orig_elements", payload.partition_parameters.include_orig_elements);
    }
    if (payload.partition_parameters.include_page_breaks !== undefined) {
        (0, encodings_js_1.appendForm)(body, "include_page_breaks", payload.partition_parameters.include_page_breaks);
    }
    if (payload.partition_parameters.include_slide_notes !== undefined) {
        (0, encodings_js_1.appendForm)(body, "include_slide_notes", payload.partition_parameters.include_slide_notes);
    }
    if (payload.partition_parameters.languages !== undefined) {
        (0, encodings_js_1.appendForm)(body, "languages", payload.partition_parameters.languages);
    }
    if (payload.partition_parameters.max_characters !== undefined) {
        (0, encodings_js_1.appendForm)(body, "max_characters", payload.partition_parameters.max_characters);
    }
    if (payload.partition_parameters.multipage_sections !== undefined) {
        (0, encodings_js_1.appendForm)(body, "multipage_sections", payload.partition_parameters.multipage_sections);
    }
    if (payload.partition_parameters.new_after_n_chars !== undefined) {
        (0, encodings_js_1.appendForm)(body, "new_after_n_chars", payload.partition_parameters.new_after_n_chars);
    }
    if (payload.partition_parameters.ocr_languages !== undefined) {
        (0, encodings_js_1.appendForm)(body, "ocr_languages", payload.partition_parameters.ocr_languages);
    }
    if (payload.partition_parameters.output_format !== undefined) {
        (0, encodings_js_1.appendForm)(body, "output_format", payload.partition_parameters.output_format);
    }
    if (payload.partition_parameters.overlap !== undefined) {
        (0, encodings_js_1.appendForm)(body, "overlap", payload.partition_parameters.overlap);
    }
    if (payload.partition_parameters.overlap_all !== undefined) {
        (0, encodings_js_1.appendForm)(body, "overlap_all", payload.partition_parameters.overlap_all);
    }
    if (payload.partition_parameters.pdf_infer_table_structure !== undefined) {
        (0, encodings_js_1.appendForm)(body, "pdf_infer_table_structure", payload.partition_parameters.pdf_infer_table_structure);
    }
    if (payload.partition_parameters.pdfminer_char_margin !== undefined) {
        (0, encodings_js_1.appendForm)(body, "pdfminer_char_margin", payload.partition_parameters.pdfminer_char_margin);
    }
    if (payload.partition_parameters.pdfminer_line_margin !== undefined) {
        (0, encodings_js_1.appendForm)(body, "pdfminer_line_margin", payload.partition_parameters.pdfminer_line_margin);
    }
    if (payload.partition_parameters.pdfminer_line_overlap !== undefined) {
        (0, encodings_js_1.appendForm)(body, "pdfminer_line_overlap", payload.partition_parameters.pdfminer_line_overlap);
    }
    if (payload.partition_parameters.pdfminer_word_margin !== undefined) {
        (0, encodings_js_1.appendForm)(body, "pdfminer_word_margin", payload.partition_parameters.pdfminer_word_margin);
    }
    if (payload.partition_parameters.similarity_threshold !== undefined) {
        (0, encodings_js_1.appendForm)(body, "similarity_threshold", payload.partition_parameters.similarity_threshold);
    }
    if (payload.partition_parameters.skip_infer_table_types !== undefined) {
        (0, encodings_js_1.appendForm)(body, "skip_infer_table_types", payload.partition_parameters.skip_infer_table_types);
    }
    if (payload.partition_parameters.split_pdf_allow_failed !== undefined) {
        (0, encodings_js_1.appendForm)(body, "split_pdf_allow_failed", payload.partition_parameters.split_pdf_allow_failed);
    }
    if (payload.partition_parameters.split_pdf_concurrency_level !== undefined) {
        (0, encodings_js_1.appendForm)(body, "split_pdf_concurrency_level", payload.partition_parameters.split_pdf_concurrency_level);
    }
    if (payload.partition_parameters.split_pdf_page !== undefined) {
        (0, encodings_js_1.appendForm)(body, "split_pdf_page", payload.partition_parameters.split_pdf_page);
    }
    if (payload.partition_parameters.split_pdf_page_range !== undefined) {
        (0, encodings_js_1.appendForm)(body, "split_pdf_page_range", payload.partition_parameters.split_pdf_page_range);
    }
    if (payload.partition_parameters.starting_page_number !== undefined) {
        (0, encodings_js_1.appendForm)(body, "starting_page_number", payload.partition_parameters.starting_page_number);
    }
    if (payload.partition_parameters.strategy !== undefined) {
        (0, encodings_js_1.appendForm)(body, "strategy", payload.partition_parameters.strategy);
    }
    if (payload.partition_parameters.table_ocr_agent !== undefined) {
        (0, encodings_js_1.appendForm)(body, "table_ocr_agent", payload.partition_parameters.table_ocr_agent);
    }
    if (payload.partition_parameters.unique_element_ids !== undefined) {
        (0, encodings_js_1.appendForm)(body, "unique_element_ids", payload.partition_parameters.unique_element_ids);
    }
    if (payload.partition_parameters.vlm_model !== undefined) {
        (0, encodings_js_1.appendForm)(body, "vlm_model", payload.partition_parameters.vlm_model);
    }
    if (payload.partition_parameters.vlm_model_provider !== undefined) {
        (0, encodings_js_1.appendForm)(body, "vlm_model_provider", payload.partition_parameters.vlm_model_provider);
    }
    if (payload.partition_parameters.xml_keep_tags !== undefined) {
        (0, encodings_js_1.appendForm)(body, "xml_keep_tags", payload.partition_parameters.xml_keep_tags);
    }
    const path = (0, url_js_1.pathToFunc)("/general/v0/general")();
    const headers = new Headers((0, primitives_js_1.compactMap)({
        Accept: options?.acceptHeaderOverride
            || "application/json;q=1, text/csv;q=0",
        "unstructured-api-key": (0, encodings_js_1.encodeSimple)("unstructured-api-key", payload["unstructured-api-key"], { explode: false, charEncoding: "none" }),
    }));
    const securityInput = await (0, security_js_1.extractSecurity)(client._options.security);
    const requestSecurity = (0, security_js_1.resolveGlobalSecurity)(securityInput);
    const context = {
        options: client._options,
        baseURL: options?.serverURL ?? client._baseURL ?? "",
        operationID: "partition",
        oAuth2Scopes: null,
        resolvedSecurity: requestSecurity,
        securitySource: client._options.security,
        retryConfig: options?.retries
            || client._options.retryConfig
            || {
                strategy: "backoff",
                backoff: {
                    initialInterval: 3000,
                    maxInterval: 720000,
                    exponent: 1.88,
                    maxElapsedTime: 1800000,
                },
                retryConnectionErrors: true,
            }
            || { strategy: "none" },
        retryCodes: options?.retryCodes || ["5xx"],
    };
    const requestRes = client._createRequest(context, {
        security: requestSecurity,
        method: "POST",
        baseURL: options?.serverURL,
        path: path,
        headers: headers,
        body: body,
        userAgent: client._options.userAgent,
        timeoutMs: options?.timeoutMs || client._options.timeoutMs || -1,
    }, options);
    if (!requestRes.ok) {
        return [requestRes, { status: "invalid" }];
    }
    const req = requestRes.value;
    const doResult = await client._do(req, {
        context,
        errorCodes: ["422", "4XX", "5XX"],
        retryConfig: context.retryConfig,
        retryCodes: context.retryCodes,
    });
    if (!doResult.ok) {
        return [doResult, { status: "request-error", request: req }];
    }
    const response = doResult.value;
    const responseFields = {
        HttpMeta: { Response: response, Request: req },
    };
    const [result] = await M.match(M.json(200, operations.PartitionResponse$inboundSchema), M.text(200, operations.PartitionResponse$inboundSchema, {
        ctype: "text/csv",
    }), M.jsonErr(422, errors.HTTPValidationError$inboundSchema), M.fail("4XX"), M.jsonErr("5XX", errors.ServerError$inboundSchema))(response, req, { extraFields: responseFields });
    if (!result.ok) {
        return [result, { status: "complete", request: req, response }];
    }
    return [result, { status: "complete", request: req, response }];
}
//# sourceMappingURL=generalPartition.js.map