// Variant Store for Optimized Search
Symphony.apiReady(['store', function(store) {

  var includedStores = ['5447', '5448', '5454'];
  
  function filterProducts(product, attributes) {
    return _.isEmpty(attributes) ? product.variants : _.filter(product.variants, function(variant) {
      return _.every(attributes, function(attr) {
        return (!attr || variant.metaMap[attr.key].id === attr.id) && !variant.soldOut;
      });
    });
  }

  function initStore(){
    var $scope = $('.product-list').scope();
    $scope.variantStore = [];
    $scope.storeOverride = false;

    $scope.setVariantStore = function(productList){
      $scope.variantStore = [];
      var hashKey = 0;
        _.each(productList, function(product) {         
          // skip product if it has no color variant attributes 
          if (!product.attrSetMap && !product.attrSetMap.color) {
            product.$$hashKey = hashKey++;
            $scope.variantStore.push(product);
            return;
          }
          // new product for each color variant attribute
          _.each(product.attrSetMap.color, function(colorAttr) {
              var newProduct = _.clone(product);
              
              // populate variants with matching color attribute
              newProduct.variants = filterProducts(newProduct, [colorAttr]);
              
              if (!newProduct.variants) {
                return;
              }

              newProduct.images[0] = newProduct.variants[0].images[0];
              newProduct.mainImage = newProduct.variants[0].images[0];
              newProduct.url = newProduct.url + '?attr=' + colorAttr.id;
              newProduct.name = newProduct.name + ' - ' + colorAttr.value;

              // reset hash key for ng-repeat, sets product's order in list
              newProduct.$$hashKey = hashKey++;
                
              // if filtering on colors makes sure variant is in color filter map
              if (!$scope.filterMap.color || $scope.filterMap.color.length === 0) {
                $scope.variantStore.push(newProduct);
              } else {
                var colorMatch = _.find($scope.filterMap.color, function(color) {
                  return colorAttr.value.toLocaleLowerCase().indexOf(color.toLocaleLowerCase()) > -1;
                });
                // if there is a match push variant
                colorMatch && $scope.variantStore.push(newProduct);
              }
          });
        });
        // replace list of product cluster with expanded product list
        $scope.productClusters = $scope.variantStore;
    };

    $scope.$watch('productClusters', function(newVal) {
      if (newVal && (newVal.length < 6) && includedStores.includes($scope.storeId)) {
        // only split products if store is less than 5 products and store description equals 'showvariants'
          $scope.setVariantStore(newVal);
      }
    });
  }
  
  // find the latest version of checkReadyState
  var checkReadyState = (Symphony.Utils && Symphony.Utils.checkReadyState) || syU.checkReadyState;
  checkReadyState(initStore,100,function() {
    return $('.product-list') && $('.product-list').scope();
  });
}]);