(function () {
  'use strict';

  const PRODUCT_COL_SELECTOR = '.col-sm-2, .col-md-6, .col-lg-2, .col-xl-2';
  const CATEGORY_SELECTOR = '.pos-categories, .pos-categories .tabs_wrapper, ul.tabs.pos-category, .tabs_wrapper';
  const PRODUCT_SELECTOR = '#product-list .product-info';

  function detectDesktopMode() {
    const productGrid = document.getElementById('product-card-grid');
    const isDesktopModeFromGrid = productGrid && productGrid.getAttribute('data-desktop-mode') === 'true';
    const bodyMode = document.body ? document.body.getAttribute('data-pos-mode') : '';
    const isDesktopModeFromBody = bodyMode === 'desktop';
    return Boolean(isDesktopModeFromGrid || isDesktopModeFromBody);
  }

  function hideCategorySection() {
    const categoryElements = document.querySelectorAll(CATEGORY_SELECTOR);
    categoryElements.forEach((el) => {
      if (!el) return;
      el.style.display = 'none';
      el.style.visibility = 'hidden';
    });
  }

  function hideProductsBySelector(selector = PRODUCT_SELECTOR) {
    document.querySelectorAll(selector).forEach((product) => {
      const col = product.closest(PRODUCT_COL_SELECTOR);
      if (col) col.style.display = 'none';
    });
  }

  function applyDesktopEntryLayout() {
    if (!detectDesktopMode()) return;
    hideCategorySection();
    hideProductsBySelector();
  }

  function initializeDesktopModeLayout() {
    if (!detectDesktopMode()) return false;
    applyDesktopEntryLayout();
    return true;
  }

  function hideProductsAfterSelection() {
    if (!detectDesktopMode()) return;
    hideProductsBySelector();
  }

  function showProductsByIds(ids = []) {
    if (!Array.isArray(ids)) return;
    hideProductsBySelector();
    ids.forEach((id) => {
      const product = document.querySelector(`#product-list .product-info[data-id="${id}"]`);
      if (!product) return;
      const col = product.closest(PRODUCT_COL_SELECTOR);
      if (col) col.style.display = 'block';
    });
  }

  function handleSearchCleared() {
    if (!detectDesktopMode()) return false;
    hideProductsBySelector();
    return true;
  }

  function handleSearchResults(results = []) {
    if (!detectDesktopMode()) return false;
    const ids = Array.isArray(results)
      ? results.map((item) => (item ? item.id : null)).filter((id) => id !== null && id !== undefined)
      : [];
    showProductsByIds(ids);
    return true;
  }

  function handleSearchItemSelected() {
    if (!detectDesktopMode()) return false;
    hideProductsBySelector();
    return true;
  }

  window.POSDesktop = window.POSDesktop || {};
  window.POSDesktop.isDesktopMode = detectDesktopMode;
  window.POSDesktop.applyDesktopEntryLayout = applyDesktopEntryLayout;
  window.POSDesktop.initializeDesktopModeLayout = initializeDesktopModeLayout;
  window.POSDesktop.hideProductsAfterSelection = hideProductsAfterSelection;
  window.POSDesktop.hideCategorySection = hideCategorySection;
  window.POSDesktop.hideProductsBySelector = hideProductsBySelector;
  window.POSDesktop.showProductsByIds = showProductsByIds;
  window.POSDesktop.handleSearchCleared = handleSearchCleared;
  window.POSDesktop.handleSearchResults = handleSearchResults;
  window.POSDesktop.handleSearchItemSelected = handleSearchItemSelected;
})();

