
import { AzureOpenAIPatameters, StabilityOption, StabilityResult, request } from "./declare";
import GptBase from "./gptbase";
export default class StabilityPlusAI extends GptBase {
    protected readonly apiKey: string;
    // protected readonly apiSetting: AzureOpenAIPatameters
    protected readonly apiOption: StabilityOption;
    constructor(apiKey: string, _urlOption: AzureOpenAIPatameters, apiOption: StabilityOption = {}) {
        super();
        this.apiKey = apiKey;
        // this.apiSetting = urlOption;
        this.apiOption = apiOption;
        // if (!this.apiSetting.endpoint.toLowerCase().startsWith('http')) {
        //     this.apiSetting.endpoint = 'https://' + this.apiSetting.endpoint;
        // }
    }
    /**
     * 请求Stable作画的接口
     */
    public async chatRequest(chatText: string, paramOption: StabilityOption, axiosOption: any = {}): Promise<StabilityResult> {
        if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
        axiosOption = Object.assign({}, axiosOption, {
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json",
            }
        })
        try {
            const requestOption = {
                ...axiosOption,
                method: "POST",
                data: {
                    "enable_hr": false,
                    "denoising_strength": paramOption.denoising_strength || this.apiOption.denoising_strength ||  0.5,
                    "firstphase_width": 0,
                    "firstphase_height": 0,
                    "hr_scale": paramOption.hr_scale || this.apiOption.hr_scale || 2,
                    "hr_upscaler": "string",
                    "hr_second_pass_steps": 0,
                    "hr_resize_x": 0,
                    "hr_resize_y": 0,
                    "prompt": chatText,
                    "styles":["string"], //[paramOption.engine || this.apiSetting.engine || "bra_v5"], //["bra_v5"],   //模型
                    "seed": paramOption.seed || this.apiOption.seed ||  -1,
                    "subseed": -1,
                    "subseed_strength": 0,
                    "seed_resize_from_h": -1,
                    "seed_resize_from_w": -1,
                    "sampler_name": paramOption.sampler || this.apiOption.sampler || "Euler a", //"Euler",
                    "batch_size": 1,
                    "n_iter": paramOption.samples || this.apiOption.samples || 1,        //生成的数量
                    "steps": paramOption.steps || this.apiOption.steps ||  20,
                    "cfg_scale": paramOption.cfg_scale || this.apiOption.cfg_scale || 7,
                    "width": paramOption.width || this.apiOption.width || 512, 
                    "height": paramOption.height || this.apiOption.height || 512,
                    "restore_faces": false,
                    "tiling": false,
                    "do_not_save_samples": false,
                    "do_not_save_grid": false,
                    "negative_prompt": paramOption.negative || ''
                },
                url: `${paramOption.endpoint}/sdapi/v1/txt2img`,
            };
            // console.log('stablity param', requestOption);
            const response: any = await request(requestOption)
            if (response.successed) {
                return { successed: true, type: 'image', data: response.data.images, };
            }
            // console.log('response result ', response.data)
            return { successed: false, ...response.data };
        } catch (error) {
            console.log('result is error ', error)
            return { successed: false, error };
        }

    }
}
