import _extends from "@babel/runtime/helpers/extends";
import { useMemo, createRef } from 'react';
import { useAllBrandsQuery, createBrand, useConfigState } from '@sentecacommerce-theme/lib';
export var useBrands = () => {
  var query = useAllBrandsQuery();
  var results = useMemo(() => {
    return _extends({}, query, {
      data: groupBrands(query.data)
    });
  }, [query]);
  return results;
};

var 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(brandRaw => {
    var _brand$slug, _brand$slug$;

    var brand = 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(groupKey => {
    var containerRef = /*#__PURE__*/createRef();
    return {
      key: groupKey,
      brands: groups[groupKey],
      containerRef
    };
  });
  return brandGroupsArray;
};

export var useFeaturedBrands = () => {
  var query = useAllBrandsQuery();
  var {
    customFieldKeys
  } = useConfigState();
  var results = useMemo(() => {
    var _query$data;

    return _extends({}, query, {
      data: (_query$data = query.data) == null ? void 0 : _query$data.filter(brandRaw => {
        var brand = createBrand(brandRaw);
        return brand == null ? void 0 : brand.getCustomFieldByKey(customFieldKeys.isFeatured);
      }).map(createBrand)
    });
  }, [query, customFieldKeys.isFeatured]);
  return results;
};
//# sourceMappingURL=brands.hook.js.map