// /AIWK_COMMON/domains/www.youtube.com/live_chat/index.js
// AIWK EXT4 v126 - URI domain module for www.youtube.com/live_chat
(function () {
  "use strict";
  const VERSION = "v127-nodrd-route-tab-switch-auth";
  const MODULE_ID = "domain.www.youtube.com.live_chat";
  if (window.__AIWK_DOMAIN_WWW_YOUTUBE_COM_LIVE_CHAT_V126__) return;
  window.__AIWK_DOMAIN_WWW_YOUTUBE_COM_LIVE_CHAT_V126__ = true;
  function log(action, detail) {
    try {
      chrome.runtime.sendMessage({
        type: "AIWK_IFRAME_AGENT_EVENT",
        action: action || "domain_event",
        frame: { href: location.href, hostname: location.hostname || "", pathname: location.pathname || "", title: document.title || "", is_top: window.top === window },
        event: Object.assign({ module_id: MODULE_ID, version: VERSION }, detail || {})
      }).catch(function () {});
    } catch (err) {}
  }
  function applyPreset() {
    const settings = {
      input_selector: "#input",
      send_selector: "#send-button button, #send-button, yt-button-renderer#send-button button",
      collect_selector: "yt-live-chat-text-message-renderer, yt-live-chat-paid-message-renderer, yt-live-chat-membership-item-renderer",
      stream: true
    };
    try {
      window.dispatchEvent(new CustomEvent("AIWK_IFRAME_CHAT_AGENT_APPLY_SETTINGS", { detail: { source: MODULE_ID, label: "www.youtube.com/live_chat", settings: settings } }));
      log("domain_preset_apply", { ok: 1, settings: settings });
    } catch (err) {
      log("domain_preset_error", { ok: 0, error: err && err.message ? err.message : String(err) });
    }
  }
  function init() {
    if (location.hostname !== "www.youtube.com") return;
    if (String(location.pathname || "").indexOf("/live_chat") !== 0) return;
    try {
      const item = { id: MODULE_ID + ".preset", label: "YouTube preset", title: "현재 live_chat iframe selector preset 적용", run: applyPreset };
      if (typeof window.AIWKRegisterDomainMenu === "function") window.AIWKRegisterDomainMenu([item]);
      else window.dispatchEvent(new CustomEvent("AIWK_DOMAIN_MENU_REGISTER", { detail: { items: [item] } }));
    } catch (err) {}
    setTimeout(applyPreset, 250);
    setTimeout(applyPreset, 1000);
  }
  window.addEventListener("AIWK_MODULE_UNLOAD", function (ev) {
    const id = ev && ev.detail && ev.detail.module_id;
    if (!id || id === MODULE_ID || id === "*") {
      window.__AIWK_DOMAIN_WWW_YOUTUBE_COM_LIVE_CHAT_V126__ = false;
      log("domain_module_unload", { ok: 1 });
    }
  });
  init();
}());
