"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.configAudioLoader = configAudioLoader;
exports.configFontLoader = configFontLoader;
exports.configImageLoader = configImageLoader;
exports.configSVGLoader = configSVGLoader;
exports.configVideoLoader = configVideoLoader;

var _nameTemplates = require("../common/nameTemplates");

// function getLoaderOptionQueryString(params) {
const ImageExtRegex = /\.jpe?g$|\.gif$|\.png|\.webp$/;
const FontExtRegex = /\.woff2|\.woff$|\.ttf$|\.eot$/;
const SVGExtRegex = /\.svg$/;
const AudioExtRegex = /\.ogg$/;
const VideoExtRegex = /\.mp4|\.webm$/;

function createAssetLoader({
  test,
  nameTemplate,
  limit
}) {
  const conf = {
    test,
    type: 'asset'
  };

  if (limit) {
    conf.parser = {
      dataUrlCondition: {
        maxSize: limit // 4 * 1024 // 4kb

      }
    };
  } else {
    conf.type = 'asset/resource';
  }

  if (nameTemplate) {
    conf.generator = {
      filename: nameTemplate
    };
  }

  return conf;
}
/**
 * @typedef {Object} LoaderConfig
 * @property {Number} enableChunkHash
 */

/**
 * @param {LoaderConfig} options
 * @returns
 */


function configImageLoader(options) {
  return createAssetLoader({
    test: ImageExtRegex,
    nameTemplate: (0, _nameTemplates.nameTemplates)('image', options),
    limit: 1000
  });
}

function configFontLoader(options) {
  return createAssetLoader({
    test: FontExtRegex,
    nameTemplate: (0, _nameTemplates.nameTemplates)('font', options),
    limit: 1000
  });
}

function configSVGLoader(options) {
  return createAssetLoader({
    test: SVGExtRegex,
    nameTemplate: (0, _nameTemplates.nameTemplates)('svg', options),
    limit: 1000
  });
}

function configAudioLoader(options) {
  return createAssetLoader({
    test: AudioExtRegex,
    nameTemplate: (0, _nameTemplates.nameTemplates)('audio', options)
  });
}

function configVideoLoader(options) {
  return createAssetLoader({
    test: VideoExtRegex,
    nameTemplate: (0, _nameTemplates.nameTemplates)('video', options)
  });
}
/*
export function createNameTemplate(enableChunkHash) {
  const ext = `${enableChunkHash ? ".[hash:20]" : ""}.[ext]`;
  return `[name]${ext}`;
}

export function createImageAndFontsAndSVGLoaders(enableChunkHash) {
  const nameTemplate = createNameTemplate(enableChunkHash);
  return [
    configImageLoader(nameTemplate),
    configFontLoader(nameTemplate),
    configSVGLoader(nameTemplate),
    configAudioLoader(nameTemplate)
  ];
}
*/

/*
export function createLoaderOptionObject(
  loaderName,
  nameTemplate,
  fallback,
  limit = 1000
) {
  return {
    loader: loaderName,
    options: {
      limit,
      name: nameTemplate,
      fallback
    }
  };
}

function configLoaderObject(filter, loaderAndOptions) {
  return {
    test: filter,
    use: loaderAndOptions
  };
}
*/