import { insert_after } from "../../../../../core/primitives/strings/insert_after.js";
import { insert_after_or_replace } from "../../../../../core/primitives/strings/insert_after_or_replace.js";
import { FP_INJECTION_POINT_ACCUMULATION } from "./FP_INJECTION_POINT_ACCUMULATION.js";
import { FP_SHADER_CHUNK_ACCUMULATION } from "./FP_SHADER_CHUNK_ACCUMULATION.js";
import { FP_SHADER_CHUNK_PREAMBLE } from "./FP_SHADER_CHUNK_PREAMBLE.js";
import { FP_SHADER_CHUNK_DECODE_PARS } from "./FP_SHADER_CHUNK_DECODE_PARS.js";
import { FP_SHADER_CHUNK_LOAD_METADATA } from "./FP_SHADER_CHUNK_LOAD_METADATA.js";

const accumulation_pars = `
        vec3 fp_normalized_screen_position = ((gl_Position.xyz / gl_Position.w)+vec3(1.0))*0.5; 
        vec3 clip_v = vec3( vec2(fp_normalized_screen_position.x, fp_normalized_screen_position.y), convert_depth_to_linear(fp_normalized_screen_position.z));
       ${FP_SHADER_CHUNK_LOAD_METADATA}
    `;

const three_chunk_accummulation_mark = '#include <lights_fragment_end>';

/**
 *
 * @param {string} base
 * @returns {string}
 */
export function fp_build_vertex_lighting_shared(base) {

    let result = FP_SHADER_CHUNK_PREAMBLE + base;

    result = insert_after(result, 'void main() {', accumulation_pars);

    result = insert_after(result, '#include <lights_pars_begin>', FP_SHADER_CHUNK_DECODE_PARS);

    result = insert_after_or_replace(
        result,
        three_chunk_accummulation_mark,
        FP_INJECTION_POINT_ACCUMULATION,
        FP_SHADER_CHUNK_ACCUMULATION
    );

    return result;
}
