"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.useFeaturedBrands = exports.useBrands = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = require("react");

var _lib = require("@sentecacommerce-theme/lib");

var useBrands = function useBrands() {
  var query = (0, _lib.useAllBrandsQuery)();
  var results = (0, _react.useMemo)(function () {
    return (0, _extends2["default"])({}, query, {
      data: groupBrands(query.data)
    });
  }, [query]);
  return results;
};

exports.useBrands = useBrands;

var groupBrands = function groupBrands(brands) {
  var groups = {
    '#': [],
    a: [],
    b: [],
    c: [],
    d: [],
    e: [],
    f: [],
    g: [],
    h: [],
    i: [],
    j: [],
    k: [],
    l: [],
    m: [],
    n: [],
    o: [],
    p: [],
    q: [],
    r: [],
    s: [],
    t: [],
    u: [],
    v: [],
    w: [],
    x: [],
    y: [],
    z: []
  };
  if (!brands) return;
  brands.forEach(function (brandRaw) {
    var _brand$slug, _brand$slug$;

    var brand = (0, _lib.createBrand)(brandRaw);
    /**
     * Index "1" is used because the slug will be like: "/adidas/b"
     */

    var firstChar = brand == null ? void 0 : (_brand$slug = brand.slug) == null ? void 0 : (_brand$slug$ = _brand$slug[1]) == null ? void 0 : _brand$slug$.toLowerCase();
    if (!brand || !firstChar) return;
    var brandGroup = groups[firstChar];

    if (brandGroup) {
      return brandGroup.push(brand);
    }

    groups['#'].push(brand);
  });
  var brandGroupsArray = Object.keys(groups).map(function (groupKey) {
    var containerRef = /*#__PURE__*/(0, _react.createRef)();
    return {
      key: groupKey,
      brands: groups[groupKey],
      containerRef: containerRef
    };
  });
  return brandGroupsArray;
};

var useFeaturedBrands = function useFeaturedBrands() {
  var query = (0, _lib.useAllBrandsQuery)();

  var _useConfigState = (0, _lib.useConfigState)(),
      customFieldKeys = _useConfigState.customFieldKeys;

  var results = (0, _react.useMemo)(function () {
    var _query$data;

    return (0, _extends2["default"])({}, query, {
      data: (_query$data = query.data) == null ? void 0 : _query$data.filter(function (brandRaw) {
        var brand = (0, _lib.createBrand)(brandRaw);
        return brand == null ? void 0 : brand.getCustomFieldByKey(customFieldKeys.isFeatured);
      }).map(_lib.createBrand)
    });
  }, [query, customFieldKeys.isFeatured]);
  return results;
};

exports.useFeaturedBrands = useFeaturedBrands;
//# sourceMappingURL=brands.hook.js.map