import { SACError as CLIError, ExitCode } from "../errors.js";
import { registerProvider } from "./store.js";
// ── Model lists ──────────────────────────────────────────────────────────────
const PROMPT_REQUIRED_MODELS = [
    'volces_seedream_5',
    'volces_seedream_4_5',
    'volces_seedream_4_0',
    'volces_seedream_3_0',
    'volces_jimeng_3_1',
    'volces_jimeng_3_0',
    'volces_seededit_3_0',
    'volces_seededit_3_0_i2i',
    'volces_jimeng_i2i_3_0',
    'volces_seededit_single_ip',
    'volces_seededit_multi_ip',
    'volces_seedream_4_5_multi_blend',
];
const PROMPT_OPTIONAL_MODELS = [
    'volces_seededit_portrait',
];
const NO_PROMPT_MODELS = [
    'volces_seededit_multi_style',
    'volces_seededit_3d_style',
    'volces_jimeng_tilesr',
];
export const VOLCES_MODELS = [
    ...PROMPT_REQUIRED_MODELS,
    ...PROMPT_OPTIONAL_MODELS,
    ...NO_PROMPT_MODELS,
];
const PROMPT_REQUIRED_SET = new Set(PROMPT_REQUIRED_MODELS);
const PROMPT_OPTIONAL_SET = new Set(PROMPT_OPTIONAL_MODELS);
const NO_PROMPT_SET = new Set(NO_PROMPT_MODELS);
// ── Helpers ─────────────────────────────────────────────────────────────────
function buildBodyEnvelope(model, params) {
    return {
        model,
        dash_scope: true,
        moderation: true,
        input: [{ params }],
        metadata: {},
    };
}
function collectImageInputs(flags) {
    const images = [];
    const single = flags.imageUrl;
    if (single)
        images.push(single);
    const many = flags.imageUrls;
    if (Array.isArray(many))
        images.push(...many);
    else if (many)
        images.push(many);
    return images;
}
function requireImageInputs(flags, model, opts = {}) {
    const images = collectImageInputs(flags);
    const min = opts.min ?? 1;
    const max = opts.max;
    if (images.length < min) {
        throw new CLIError(`Model "${model}" requires ${opts.hint ?? '--image-url'}${min > 1 ? ` (${min}+ images)` : ''}.`, ExitCode.USAGE);
    }
    if (max !== undefined && images.length > max) {
        throw new CLIError(`Model "${model}" supports at most ${max} input image${max === 1 ? '' : 's'}.`, ExitCode.USAGE);
    }
    return images;
}
function requireSingleImage(flags, model, fieldName = '--image-url') {
    return requireImageInputs(flags, model, { min: 1, max: 1, hint: fieldName })[0];
}
function maybeSize(flags) {
    if (flags.size)
        return flags.size;
    if (flags.width && flags.height)
        return `${flags.width}x${flags.height}`;
    return undefined;
}
function buildSeedream45FamilyParams(prompt, flags) {
    const params = { prompt };
    const images = collectImageInputs(flags);
    const size = maybeSize(flags);
    if (images.length === 1) {
        params.image = images[0];
    }
    else if (images.length > 1) {
        params.image = images;
    }
    if (size)
        params.size = size;
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    const batchCount = flags.n;
    if (batchCount !== undefined && batchCount > 1) {
        params.sequential_image_generation = 'auto';
        params.sequential_image_generation_options = { max_images: batchCount };
    }
    return params;
}
function buildSeedream3Params(prompt, flags) {
    const params = { prompt };
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    if (flags.scale !== undefined)
        params.scale = flags.scale;
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    return params;
}
function buildJimengTextParams(prompt, flags) {
    const params = { prompt };
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    return params;
}
function buildSeededitBasicParams(model, prompt, flags) {
    const image = requireSingleImage(flags, model);
    const params = {
        prompt,
        image_urls: [image],
    };
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    if (flags.scale !== undefined)
        params.scale = flags.scale;
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    return params;
}
function buildSeededit30I2IParams(model, prompt, flags) {
    const image = requireSingleImage(flags, model);
    const params = {
        prompt,
        image,
    };
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    if (flags.scale !== undefined)
        params.scale = flags.scale;
    const size = maybeSize(flags);
    if (size)
        params.size = size;
    return params;
}
function buildSeededitMultiIpParams(model, prompt, flags) {
    const images = requireImageInputs(flags, model, { min: 1, max: 5, hint: '--image-url/--image-urls' });
    const params = {
        prompt,
        image_urls: images,
    };
    if (flags.seed !== undefined)
        params.seed = flags.seed;
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    const refTypes = flags.refType;
    if (Array.isArray(refTypes))
        params.ref_type_list = refTypes;
    else if (refTypes)
        params.ref_type_list = [refTypes];
    return params;
}
function buildSeededit3dStyleParams(model, flags) {
    const image = requireSingleImage(flags, model);
    const reqKey = flags.reqKey;
    const subReqKey = flags.subReqKey;
    if (!reqKey) {
        throw new CLIError(`Model "${model}" requires --req-key.`, ExitCode.USAGE);
    }
    if (!subReqKey) {
        throw new CLIError(`Model "${model}" requires --sub-req-key.`, ExitCode.USAGE);
    }
    return {
        req_key: reqKey,
        sub_req_key: subReqKey,
        image_urls: [image],
        return_url: true,
    };
}
function buildSeededitMultiStyleParams(model, flags) {
    const image = requireSingleImage(flags, model);
    const templateId = flags.templateId;
    if (!templateId) {
        throw new CLIError(`Model "${model}" requires --template-id.`, ExitCode.USAGE);
    }
    const params = {
        image_input1: image,
        template_id: templateId,
    };
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    return params;
}
function buildSeededitPortraitParams(model, prompt, flags) {
    const image = requireSingleImage(flags, model);
    const params = {
        image_input: image,
    };
    if (prompt)
        params.prompt = prompt;
    if (flags.width !== undefined)
        params.width = flags.width;
    if (flags.height !== undefined)
        params.height = flags.height;
    if (flags.gpen !== undefined)
        params.gpen = flags.gpen;
    if (flags.skin !== undefined)
        params.skin = flags.skin;
    if (flags.skinUnifi !== undefined)
        params.skin_unifi = flags.skinUnifi;
    if (flags.genMode !== undefined)
        params.gen_mode = flags.genMode;
    if (flags.seed !== undefined)
        params.seed = String(flags.seed);
    return params;
}
function buildJimengTileSrParams(model, flags) {
    const image = requireSingleImage(flags, model);
    const params = {
        image_url: image,
    };
    if (flags.resolution !== undefined)
        params.resolution = flags.resolution;
    if (flags.scale !== undefined)
        params.scale = flags.scale;
    return params;
}
function buildSeedreamMultiBlendParams(model, prompt, flags) {
    const images = requireImageInputs(flags, model, { min: 2, max: 14, hint: '--image-urls' });
    const params = {
        prompt,
        image: images,
    };
    const size = maybeSize(flags);
    if (size)
        params.size = size;
    return params;
}
function buildVolcesParams(model, prompt, flags) {
    switch (model) {
        case 'volces_seedream_5':
        case 'volces_seedream_4_5':
        case 'volces_seedream_4_0':
            return buildSeedream45FamilyParams(prompt, flags);
        case 'volces_seedream_3_0':
            return buildSeedream3Params(prompt, flags);
        case 'volces_jimeng_3_1':
        case 'volces_jimeng_3_0':
            return buildJimengTextParams(prompt, flags);
        case 'volces_seededit_3_0':
        case 'volces_seededit_single_ip':
            return buildSeededitBasicParams(model, prompt, flags);
        case 'volces_seededit_3_0_i2i':
            return buildSeededit30I2IParams(model, prompt, flags);
        case 'volces_jimeng_i2i_3_0':
            return buildSeededitBasicParams(model, prompt, flags);
        case 'volces_seededit_multi_ip':
            return buildSeededitMultiIpParams(model, prompt, flags);
        case 'volces_seededit_3d_style':
            return buildSeededit3dStyleParams(model, flags);
        case 'volces_seededit_multi_style':
            return buildSeededitMultiStyleParams(model, flags);
        case 'volces_seededit_portrait':
            return buildSeededitPortraitParams(model, prompt, flags);
        case 'volces_jimeng_tilesr':
            return buildJimengTileSrParams(model, flags);
        case 'volces_seedream_4_5_multi_blend':
            return buildSeedreamMultiBlendParams(model, prompt, flags);
        default:
            throw new CLIError(`Unsupported Volces image model "${model}".`, ExitCode.USAGE);
    }
}
// ── Registration ─────────────────────────────────────────────────────────────
registerProvider({
    provider: 'volces',
    category: 'image',
    models: VOLCES_MODELS,
    requiresPrompt(model) {
        if (PROMPT_REQUIRED_SET.has(model))
            return true;
        if (PROMPT_OPTIONAL_SET.has(model))
            return false;
        if (NO_PROMPT_SET.has(model))
            return false;
        return true;
    },
    buildBody(model, prompt, flags) {
        return buildBodyEnvelope(model, buildVolcesParams(model, prompt, flags));
    },
});
//# sourceMappingURL=volces.js.map