const suggestions = [
  {
    value: 'lintel',
    url: '/'
  },
  {
    value: 'lining paper',
    url: '/'
  },
  {
    value: 'line',
    url: '/'
  },
  {
    value: 'light',
    url: '/'
  },
  {
    value: 'lime',
    url: '/'
  }
]
const products = [
  {
    value: 'Rangemaster Classic Deluxe 90 Ceramic Range Cooker - Black with Chrome Trim Rangemaster Classic Deluxe 90 Ceramic Range Cooker - Black with Chrome Trim',
    image: 'https://media.wickes.co.uk/is/image/wickes/GPID_5000085053_00?$thumbnail$',
    url: '/',
    rating: "4.2",
    ratingQuantity: "31",
    price: "£79.99",
    priceExcVat: "£78.99",
    oldPrice: "£99.99",
    oldPriceExcVat: "£98.99",
    basePrice: true,
    baseOldPrice: true,
  },
  {
    value: 'Rangemaster',
    image: 'https://media.wickes.co.uk/is/image/wickes/G0116_121330_00?$thumbnail$',
    url: '/',
    ratingQuantity: "21",
    price: "£79.99",
    priceExcVat: "£78.99",
    oldPrice: "£99.99",
    oldPriceExcVat: "£98.99",
    basePrice: true,
    baseOldPrice: false,
  },
  {
    value: 'Rangemaster Classic Deluxe 90 Ceramic Range Cooker - Black with Chrome Trim Rangemaster Classic Deluxe 90 Ceramic Range Cooker - Black with Chrome Trim',
    image: 'https://media.wickes.co.uk/is/image/wickes/normal/Footwear-Tough-Grit-Larch-Wellington-Size-8~GPID_1100568113_00',
    url: '/',
    rating: "3.0",
    ratingQuantity: "12",
    price: "£79.99",
    priceExcVat: "£78.99",
    oldPrice: "£99.99",
    oldPriceExcVat: "£98.99",
    basePrice: false,
    baseOldPrice: false,
  },
  {
    value: 'Rangemaster Classic Deluxe 90 Ceramic Range Cooker',
    image: 'https://media.wickes.co.uk/is/image/wickes/normal/Footwear-Tough-Grit-Oak-Boot-7~GPID_1100568107_00',
    url: '/',
    rating: "2.9",
    ratingQuantity: "5",
    price: "£79.99",
    priceExcVat: "£78.99",
    basePrice: false,
    baseOldPrice: false,
  }
];
const contentPages = [
  {
    value: 'Rangemaster',
    image: 'https://media.wickes.co.uk/is/image/wickes/O0032_109451_00?$thumbnail$',
    url: '/'
  },
  {
    value: 'Rangemaster Classic Deluxe 90 Ceramic Range Cooker',
    image: 'https://media.wickes.co.uk/is/image/wickes/G0116_121330_00?$thumbnail$',
    url: '/'
  },
  {
    value: 'Rangemaster Classic Deluxe 90 Ceramic Range Cooker',
    image: 'https://media.wickes.co.uk/is/image/wickes/normal/Footwear-Tough-Grit-Oak-Boot-7~GPID_1100568107_00',
    url: '/'
  },
  {
    value: 'Rangemaster',
    image: 'https://media.wickes.co.uk/is/image/wickes/normal/Footwear-Tough-Grit-Alder-Trainer-7~GPID_1100568095_00',
    url: '/'
  }
];

function initLocalSearchBar () {
  if (!Wick?.GlobalSearch?.SEARCH_PRODUCTS_EVENT) {
    return;
  }

  $(window).on(Wick.GlobalSearch.SEARCH_PRODUCTS_EVENT, function ({ detail: { resolve, reject, payload }}) {
    setTimeout(() => {
      let searchResults = {};

      switch (payload.value.length) {
        case 2:
          searchResults = {suggestions, products, contentPages};
          break;
        case 3:
          searchResults = {suggestions, products};
          break;
        case 4:
          searchResults = {suggestions, contentPages};
          break;
        case 5:
          searchResults = {suggestions};
          break;
        case 6:
          searchResults = {products};
          break;
        case 7:
          searchResults = {contentPages};
          break;
      }

      resolve(searchResults);
    }, 500);
  });
};

$(document).ready(function () {
  initLocalSearchBar();
});
