import { k as h, aI as f, m as e, v as x, q as g, B as y } from "./publicApi-fkG2o0UD.js";
import u, { useEffect as _ } from "react";
import { d as C, E, a as v } from "./EnterpriseViewOnlyPreset-CW0maUhb.js";
import { w as n, g as S } from "./utils-BXswP1HG.js";
import { F as A } from "./consts-C-TYhRis.js";
const R = "Open in Search", w = g(y)`
    max-width: fit-content;
    font-size: ${x.fontSize};
    align-content: flex-end;
    place-self: end;
    grid-column: 2;
    grid-row: 4;
    bottom: 8px;
`, j = ({
  dashboardDefinition: t
}) => {
  const r = u.useRef(), a = h(), d = u.useCallback((i) => {
    r.current = i;
  }, []);
  return _(() => {
    n(
      '[data-input-id="errors_tab_input"][data-input-type="input.timerange"]',
      "#errors_tab_description_viz"
    ), n(
      '[data-input-id="errors_type_input"][data-input-type="input.multiselect"]',
      "#errors_tab_errors_list_viz"
    ), n(
      "#open_search_error_events_tab_with_types",
      "#errors_tab_errors_list_viz"
    );
  }, []), _(() => {
    if (!r?.current)
      return () => {
      };
    const m = f.create(
      {
        search: `index=_internal source=*${a.meta.restRoot}* log_level=ERROR | dedup exc_l | table exc_l`,
        earliest_time: "0",
        // all time
        latest_time: "now"
      },
      { cache: !0, cancelOnUnload: !0 }
      // default cache 10min = 600 in seconds
    ).getResults().subscribe((l) => {
      if (l?.results?.length) {
        const o = JSON.parse(JSON.stringify(t));
        o.inputs.errors_type_input.options.items = [
          {
            label: "All",
            value: "*"
          }
        ], l.results.sort((s, b) => {
          const c = s.exc_l.toUpperCase(), p = b.exc_l.toUpperCase();
          return c < p ? -1 : c > p ? 1 : 0;
        }).forEach(
          (s) => o.inputs.errors_type_input.options.items.push({
            label: s.exc_l,
            value: `"${s.exc_l}"`
          })
        ), r.current?.updateDefinition(o);
      }
    });
    return () => {
      m.unsubscribe();
    };
  }, [t, a.meta.restRoot, r]), t ? /* @__PURE__ */ e.jsx(
    C.DashboardContextProvider,
    {
      preset: E,
      initialDefinition: t,
      featureFlags: A,
      children: /* @__PURE__ */ e.jsxs(e.Fragment, { children: [
        /* @__PURE__ */ e.jsx(
          w,
          {
            id: "open_search_error_events_tab_with_types",
            label: R,
            openInNewContext: !0,
            onClick: () => document.querySelector(
              '#errors_tab_errors_list_viz [data-test="open-search-button"]'
            )?.click()
          }
        ),
        /* @__PURE__ */ e.jsx(
          v.DashboardCore,
          {
            width: "100%",
            height: "auto",
            actionMenus: S("error"),
            dashboardCoreApiRef: d
          }
        )
      ] })
    }
  ) : null;
};
export {
  j as ErrorDashboard,
  j as default
};
