// Компактный поиск. Заменяет содержимое Search-fixed.js.
(function () {
  'use strict';
  // Не показывать поиск на странице входа, включая ?required=true.
  if (/^\/cms\/system\/login\/?$/.test(window.location.pathname)) return;
  if (/editMode[=/]1/.test(location.href)) return;
  if (document.getElementById('school-search-compact-style')) return;
  const style = document.createElement('style');
  style.id = 'school-search-compact-style';
  style.textContent = `
    #school-search-compact { position:fixed; top:6px; right:48px; z-index:1000;
      display:flex; align-items:center; gap:0; margin:0; padding:0;
      width:40px; height:40px; box-sizing:border-box; background:#fff;
      border-radius:22px; box-shadow:0 2px 10px #0002; color:#222; }
    #school-search-compact.is-open { width:min(420px, calc(100vw - 130px)); }
    #school-search-compact button { flex:0 0 40px; width:40px; height:40px;
      display:flex; align-items:center; justify-content:center; padding:0;
      margin:0; border:0; border-radius:50%; background:#fff; color:#655579;
      cursor:pointer; }
    #school-search-compact button svg { width:21px; height:21px; }
    #school-search-compact input { flex:1; min-width:0; width:0; height:34px;
      padding:5px 0 5px 14px; margin:0; border:0; border-radius:22px;
      box-shadow:none; background:#fff !important; color:#222 !important;
      font:14px Arial,sans-serif; outline:none; }
    #school-search-compact input::placeholder { color:#777; opacity:1; }
    #school-search-compact input[hidden] { display:none !important; }
    #school-search-compact button:focus-visible { outline:2px solid #7955a1; outline-offset:2px; }
    #school-search-compact:focus-within { box-shadow:0 0 0 2px #7955a1; }
    @media (max-width:380px) { #school-search-compact { right:42px; }
      #school-search-compact.is-open { width:calc(100vw - 118px); } }
  `;
  document.head.appendChild(style);
  function mount() {
    if (document.getElementById('school-search-compact')) return;
    const form = document.createElement('form');
    form.id = 'school-search-compact';
    form.method = 'get';
    form.action = '/pl/search/search-lesson';
    form.setAttribute('role', 'search');
    const input = document.createElement('input');
    input.type = 'search'; input.name = 'q'; input.id = 'school-search-query';
    input.placeholder = 'Найти урок или тренинг';
    input.setAttribute('aria-label', 'Название урока или тренинга');
    input.hidden = true;
    const button = document.createElement('button');
    button.type = 'button';
    button.setAttribute('aria-controls', input.id);
    button.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><circle cx="10.5" cy="10.5" r="6.5"/><path d="m16 16 5 5"/></svg>';
    function setOpen(open, focus) {
      form.classList.toggle('is-open', open);
      input.hidden = !open;
      button.setAttribute('aria-expanded', String(open));
      button.setAttribute('aria-label', open ? 'Найти' : 'Открыть поиск');
      button.title = open ? 'Найти — или нажмите Enter' : 'Поиск уроков и тренингов';
      if (focus) (open ? input : button).focus();
    }
    function search() {
      const q = input.value.trim();
      if (q.length < 3) {
        input.setCustomValidity('Введите не менее 3 символов');
        input.reportValidity();
        return;
      }
      window.location.assign('/pl/search/search-lesson?q=' + encodeURIComponent(q));
    }
    button.addEventListener('click', function () {
      if (!form.classList.contains('is-open')) setOpen(true, true);
      else search();
    });
    form.addEventListener('submit', function (event) { event.preventDefault(); search(); });
    input.addEventListener('input', function () { input.setCustomValidity(''); });
    form.addEventListener('keydown', function (event) {
      if (event.key === 'Escape') { event.preventDefault(); setOpen(false, true); }
    });
    document.addEventListener('click', function (event) {
      if (!form.contains(event.target)) setOpen(false, false);
    });
    form.append(input, button);
    setOpen(false, false);
    document.body.appendChild(form);
  }
  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', mount, {once:true});
  else mount();
})();
