"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var pattern_gl_Position = 'gl_Position.*(?![^]*gl_Position)';
var pattern_Main = '[^\\w]*main[^\\w]*(void)?[^\\w]*{';
var rePosition = new RegExp(pattern_gl_Position);
var reMain = new RegExp(pattern_Main);
var _default = {
  patchMaterialForLogDepthSupport: function patchMaterialForLogDepthSupport(material) {
    // Check if the shader does not already use the log depth buffer
    if (material.vertexShader.includes('USE_LOGDEPTHBUF') || material.vertexShader.includes('logdepthbuf_pars_vertex')) {
      return;
    }

    // Add vertex shader log depth buffer header
    material.vertexShader = "#include <logdepthbuf_pars_vertex>\n#define EPSILON 1e-6\n".concat(material.vertexShader);
    // Add log depth buffer code snippet after last gl_Position modification
    var re = rePosition.exec(material.vertexShader);
    var idx = re[0].length + re.index;
    material.vertexShader = "".concat(material.vertexShader.slice(0, idx), "\n#include <logdepthbuf_vertex>\n").concat(material.vertexShader.slice(idx));

    // Add fragment shader log depth buffer header
    material.fragmentShader = "#include <itowns/precision_qualifier\n".concat(material.fragmentShader);
    material.fragmentShader = "#include <logdepthbuf_pars_fragment>\n".concat(material.fragmentShader);
    // Add log depth buffer code snippet at the first line of the main function
    re = reMain.exec(material.fragmentShader);
    idx = re[0].length + re.index;
    material.fragmentShader = "".concat(material.fragmentShader.slice(0, idx), "\n#include <logdepthbuf_fragment>\n").concat(material.fragmentShader.slice(idx));
    material.defines = {
      USE_LOGDEPTHBUF: 1,
      USE_LOGDEPTHBUF_EXT: 1
    };
  },
  // adapted from unrollLoops in WebGLProgram
  unrollLoops: function unrollLoops(string, defines) {
    // look for a for loop with an unroll_loop pragma
    // The detection of the scope of the for loop is hacky as it does not support nested scopes

    function replace(match, start, end, snippet) {
      var unroll = '';
      start = start in defines ? defines[start] : parseInt(start, 10);
      end = end in defines ? defines[end] : parseInt(end, 10);
      for (var i = start; i < end; i++) {
        unroll += snippet.replace(/\bi\b/g, " ".concat(i, " "));
      }
      return unroll;
    }
    return string.replace(/#pragma unroll_loop\s+for\s*\(\s*int\s+i\s*=\s*([\w\d]+);\s*i\s+<\s+([\w\d]+);\s*i\s*\+\+\s*\)\s*\{\n([^}]*)\}/g, replace);
  }
};
exports["default"] = _default;