/**
 * @file align model
 */

import { Runner, env } from '@paddlejs/paddlejs-core';
import '@paddlejs/paddlejs-backend-webgl';
interface ModelConfig {
    path: string;
    mean?: number[];
    std?: number[];
    needPreheat?: boolean;
}

interface alignMap {
    [key: string]: string
}

let alignMap = null as any;
let runner = null as Runner;
const inputShapeW = 128;
const inputShapeH = 128;

export async function load(config: ModelConfig) {
    const {
        path,
        mean,
        std,
        needPreheat = true
    } = config;

    (window as any).runner = runner = new Runner({
        modelPath: path,
        feedShape: {
            fw: inputShapeW,
            fh: inputShapeH
        },
        targetSize: {
            height: inputShapeH,
            width: inputShapeW
        },
        mean: mean || [0.5, 0.5, 0.5],
        std: std || [1, 1, 1],
        needPreheat
    });

    env.set('debug', true);
    // (window as any).layerName = 'elementwise_add_6';

    const result = await runner.init();
}


export async function predict(data) {
    console.log('align before')
    const result = await runner.predictWithFeed(data);

    if (!result || !result.length) {
        return [];
    }
    console.log('align after')


    const ptr = result[0]['save_infer_model/scale_0.tmp_0'];
    // arr 144
    const coords = new Array(72);
    for (let i = 0; i < 72; i++) {
        const x = ptr[i * 2];
        const y = ptr[i * 2 + 1];
        coords[i] = [x, y];
    }
    return coords;


}

