'use strict';

var getSymbolsInPatternWithPosition = require('../getSymbolsInPatternWithPosition/getSymbolsInPatternWithPosition.js');

const formatWithPattern = (value, pattern) => {
  if (!value || value === '') {
    return '';
  }
  const valueArray = value.toString().split('');
  const patternWithSymbolsPosition = getSymbolsInPatternWithPosition(pattern);
  let patternSymbol = [];
  // valueArray.length increments during the cycle cause we are adding new elements.
  for (let index = 0; index < valueArray.length; index += 1) {
    patternSymbol = patternWithSymbolsPosition.filter(pattern => pattern.index === index);
    // Add pattern's symbol at n position
    if (patternSymbol.length === 1) {
      const last = patternSymbol.pop();
      if (last) {
        valueArray.splice(index, 0, last.symbol);
      }
    }
  }
  return valueArray.join('');
};

module.exports = formatWithPattern;
//# sourceMappingURL=formatWithPattern.js.map
