import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _extends from "@babel/runtime/helpers/extends";

var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6, _templateObject7, _templateObject8, _templateObject9, _templateObject10;

import React, { useMemo } from 'react';
import styled, { css } from 'styled-components';
import Masonry from 'react-masonry-css';
import { Link, SectionItemDTOTypeEnum, customFieldsToMap, useConfigState } from '@sentecacommerce-theme/lib';
import { FeaturedBrands } from '../FeaturedBrands';
export var BrandsSectionList = (_ref) => {
  var {
    sections,
    isActive,
    defaultFeaturedBrandsImage
  } = _ref;
  var {
    customFieldKeys
  } = useConfigState();
  var {
    brands,
    featuredBrands
  } = useMemo(() => {
    var _sections$, _sections$$items, _sections$$items$find, _data$items, _data$items2;

    var data = sections == null ? void 0 : (_sections$ = sections[0]) == null ? void 0 : (_sections$$items = _sections$.items) == null ? void 0 : (_sections$$items$find = _sections$$items.find(item => item.type === SectionItemDTOTypeEnum.Brand)) == null ? void 0 : _sections$$items$find.data;
    var result = data == null ? void 0 : (_data$items = data.items) == null ? void 0 : _data$items.reduce((map, brand) => {
      var _brand$name, _brand$name$trim, _brand$name$trim$;

      var char = brand == null ? void 0 : (_brand$name = brand.name) == null ? void 0 : (_brand$name$trim = _brand$name.trim()) == null ? void 0 : (_brand$name$trim$ = _brand$name$trim[0]) == null ? void 0 : _brand$name$trim$.toLowerCase();
      if (!char) return map;
      var currentArray = map[char] || [];
      if (currentArray.length > 17) return map;
      return _extends({}, map, {
        [char]: [...currentArray, brand]
      });
    }, {});
    var cols = [];
    var brandKeys = Object.keys(result || {}).sort();
    brandKeys.forEach(brandKey => {
      var brandsCollection = result[brandKey];
      cols.push({
        key: brandKey,
        brands: brandsCollection
      });
    });
    var featured = (data == null ? void 0 : (_data$items2 = data.items) == null ? void 0 : _data$items2.filter(b => {
      var customField = customFieldsToMap(b.custom || []);
      return customField[customFieldKeys.isFeatured];
    })) || [];
    return {
      brands: cols,
      featuredBrands: featured
    };
  }, [sections, customFieldKeys.isFeatured]);
  return /*#__PURE__*/React.createElement(Container, null, /*#__PURE__*/React.createElement(Relative, null, /*#__PURE__*/React.createElement(Masonry, {
    breakpointCols: 5,
    className: "Masonry--grid",
    columnClassName: "Masonry--column"
  }, brands.map((data, index) => {
    var _data$brands;

    return /*#__PURE__*/React.createElement(BrandContainer, {
      isActive: isActive,
      key: "Brand__" + index
    }, /*#__PURE__*/React.createElement(DropdownItem, {
      as: "span",
      isHeader: true
    }, data.key), (_data$brands = data.brands) == null ? void 0 : _data$brands.map(brand => {
      return /*#__PURE__*/React.createElement(Link, {
        key: 'Sub DropdownItem-' + brand.name + brand.slug,
        href: brand.slug || '/',
        passHref: true
      }, /*#__PURE__*/React.createElement(DropdownItem, null, brand.name));
    }));
  }))), /*#__PURE__*/React.createElement(FeaturedBrandsContainer, {
    isActive: isActive
  }, Boolean(featuredBrands.length) && /*#__PURE__*/React.createElement(FeaturedBrands, {
    isActive: isActive,
    brands: featuredBrands,
    defaultBackground: defaultFeaturedBrandsImage
  })));
};
var Relative = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  position: relative;\n"])));
var Animation = styled.div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n  opacity: 0;\n  transform: translateY(-1rem);\n  transition: transform 0.1s, opacity 0.1s;\n  transition-delay: 0s;\n\n  ", "\n"])), props => props.isActive && css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["\n      opacity: 1;\n      transform: translateY(0);\n      transition: transform 0.3s ease, opacity 0.2s linear;\n      transition-delay: 0.2s;\n    "]))));
var AllBrands = styled(Animation).attrs({
  className: 'd-flex align-items-center',
  as: 'a'
})(_templateObject4 || (_templateObject4 = _taggedTemplateLiteralLoose(["\n  font-size: 1.4rem;\n  line-height: 1.4;\n  padding-left: 2rem;\n  cursor: pointer;\n  color: ", ";\n  font-weight: bold;\n\n  position: absolute;\n  bottom: 0;\n  right: 0;\n  width: 20%;\n  text-align: left;\n\n  & svg {\n    width: 1.8rem;\n    margin-left: 0.4rem;\n  }\n"])), (_ref2) => {
  var {
    theme
  } = _ref2;
  return theme.palette.primary;
});
var Container = styled.div.attrs({
  className: 'mx-auto'
})(_templateObject5 || (_templateObject5 = _taggedTemplateLiteralLoose(["\n  width: calc((100% / 6) * 4);\n\n  & .Masonry--grid {\n    display: -webkit-box; /* Not needed if autoprefixing */\n    display: -ms-flexbox; /* Not needed if autoprefixing */\n    display: flex;\n    margin-left: -2rem; /* gutter size offset */\n    width: auto;\n  }\n\n  & .Masonry--column {\n    padding-left: 2rem; /* gutter size */\n    background-clip: padding-box;\n  }\n"])));
var BrandContainer = styled(Animation)(_templateObject6 || (_templateObject6 = _taggedTemplateLiteralLoose([""])));
var FeaturedBrandsContainer = styled(Animation)(_templateObject7 || (_templateObject7 = _taggedTemplateLiteralLoose(["\n  width: 100%;\n  height: 14rem;\n  z-index: 100;\n  background: ", ";\n"])), (_ref3) => {
  var {
    theme
  } = _ref3;
  return theme.palette.secondary;
});
export var DropdownItem = styled.a(_templateObject8 || (_templateObject8 = _taggedTemplateLiteralLoose(["\n  font-size: 1.4rem;\n  line-height: 1.4;\n  cursor: pointer;\n  color: ", ";\n  font-weight: 500;\n  text-align: left;\n  display: block;\n  text-decoration: none;\n  text-transform: uppercase;\n\n  ", "\n\n  ", "\n"])), (_ref4) => {
  var {
    theme
  } = _ref4;
  return theme.palette.hint;
}, props => !props.isHeader && css(_templateObject9 || (_templateObject9 = _taggedTemplateLiteralLoose(["\n      :hover {\n        color: ", ";\n        transition: color 0.2s;\n      }\n    "])), props.theme.palette.primary), props => props.isHeader && css(_templateObject10 || (_templateObject10 = _taggedTemplateLiteralLoose(["\n      margin-top: 1.3rem;\n      margin-bottom: 1.3rem;\n      color: ", ";\n      font-size: 1.6rem;\n    "])), props.theme.palette.primary));
//# sourceMappingURL=index.js.map