import { _ as __nuxt_component_0 } from './Card-BkfpdQZ7.mjs';
import { a as __nuxt_component_1 } from './Button-Dgqz5RR8.mjs';
import { _ as __nuxt_component_2, u as useApi } from './useApi-CFWxoIea.mjs';
import { _ as __nuxt_component_3 } from './FormGroup-UXO0FHfr.mjs';
import { _ as __nuxt_component_5 } from './Input-ckU7qZlb.mjs';
import { _ as __nuxt_component_4 } from './Select-ByGZz9s4.mjs';
import { _ as __nuxt_component_10, s as setInterval } from './interval-Dye37XM9.mjs';
import { _ as __nuxt_component_7 } from './Modal-KQ_hNOLI.mjs';
import { defineComponent, ref, watch, mergeProps, withCtx, unref, isRef, createVNode, openBlock, createBlock, createCommentVNode, toDisplayString, createTextVNode, nextTick, useSSRContext } from 'vue';
import { ssrRenderAttrs, ssrRenderComponent, ssrInterpolate } from 'vue/server-renderer';
import { _ as _export_sfc } from './_plugin-vue_export-helper-1tPrXgE0.mjs';
import 'tailwind-merge';
import './selectMenu-B2FUotwL.mjs';
import './server.mjs';
import '../nitro/nitro.mjs';
import 'node:path';
import 'node:module';
import 'node:fs';
import 'node:os';
import 'dotenv';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:crypto';
import 'node:async_hooks';
import 'node:url';
import 'node:net';
import '@iconify/utils';
import 'consola';
import 'vue-router';
import '@vueuse/core';
import '@iconify/vue';
import '../routes/renderer.mjs';
import 'vue-bundle-renderer/runtime';
import 'unhead/server';
import 'devalue';
import 'unhead/utils';
import 'unhead/plugins';
import './nuxt-link-B3UD8izG.mjs';
import './index-Dy4Tu7MB.mjs';
import '@iconify/utils/lib/css/icon';
import 'perfect-debounce';

const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "logs",
  __ssrInlineRender: true,
  setup(__props) {
    const api = useApi();
    const data = ref(null);
    const loading = ref(false);
    const loadError = ref("");
    const searchInput = ref("");
    const query = ref("");
    const limit = ref(1e3);
    const limitOptions = [
      { label: "200", value: 200 },
      { label: "500", value: 500 },
      { label: "1,000", value: 1e3 },
      { label: "5,000", value: 5e3 },
      { label: "10,000", value: 1e4 }
    ];
    const autoPoll = ref(true);
    const confirmClear = ref(false);
    const clearing = ref(false);
    const viewer = ref(null);
    let pollHandle = null;
    let debounceHandle = null;
    async function reload(opts = {}) {
      loading.value = true;
      loadError.value = "";
      try {
        const params = new URLSearchParams();
        params.set("limit", String(limit.value));
        if (query.value) params.set("q", query.value);
        const url = `/api/logs?${params.toString()}`;
        data.value = await api(url);
        if (!opts.keepScroll) {
          await nextTick();
          scrollToBottom();
        }
      } catch (err) {
        loadError.value = err instanceof Error ? err.message : String(err);
      } finally {
        loading.value = false;
      }
    }
    function scrollToBottom() {
      const el = viewer.value;
      if (el) el.scrollTop = el.scrollHeight;
    }
    async function clearLog() {
      clearing.value = true;
      try {
        await api("/api/logs", { method: "DELETE" });
        confirmClear.value = false;
        await reload();
      } catch (err) {
        loadError.value = err instanceof Error ? err.message : String(err);
      } finally {
        clearing.value = false;
      }
    }
    watch(searchInput, (val) => {
      if (debounceHandle) clearTimeout(debounceHandle);
      debounceHandle = setTimeout(() => {
        query.value = val;
      }, 300);
    });
    watch([query, limit], () => {
      reload();
    });
    function startPolling() {
      stopPolling();
      if (!autoPoll.value) return;
      pollHandle = setInterval();
    }
    function stopPolling() {
      if (pollHandle) {
        clearInterval(pollHandle);
        pollHandle = null;
      }
    }
    watch(autoPoll, startPolling);
    return (_ctx, _push, _parent, _attrs) => {
      const _component_UCard = __nuxt_component_0;
      const _component_UButton = __nuxt_component_1;
      const _component_UAlert = __nuxt_component_2;
      const _component_UFormGroup = __nuxt_component_3;
      const _component_UInput = __nuxt_component_5;
      const _component_USelect = __nuxt_component_4;
      const _component_UCheckbox = __nuxt_component_10;
      const _component_UModal = __nuxt_component_7;
      _push(`<div${ssrRenderAttrs(mergeProps({ class: "logs-page" }, _attrs))} data-v-d2744000>`);
      _push(ssrRenderComponent(_component_UCard, null, {
        header: withCtx((_, _push2, _parent2, _scopeId) => {
          var _a, _b, _c, _d, _e, _f;
          if (_push2) {
            _push2(`<div class="flex items-center justify-between gap-3 flex-wrap" data-v-d2744000${_scopeId}><div data-v-d2744000${_scopeId}><h1 class="text-lg font-semibold" data-v-d2744000${_scopeId}>Logs</h1><p class="text-xs text-gray-500 mt-1" data-v-d2744000${_scopeId}><code data-v-d2744000${_scopeId}>${ssrInterpolate(((_a = unref(data)) == null ? void 0 : _a.path) || "\u2014")}</code>`);
            if (unref(data) && !unref(data).exists) {
              _push2(`<span class="ml-2 text-amber-600" data-v-d2744000${_scopeId}>not found</span>`);
            } else if (unref(data)) {
              _push2(`<span class="ml-2 text-gray-500" data-v-d2744000${_scopeId}>${ssrInterpolate(unref(data).total.toLocaleString())} line(s) total`);
              if (unref(data).truncated) {
                _push2(`<span data-v-d2744000${_scopeId}>, tailing last ${ssrInterpolate(unref(data).limit.toLocaleString())}</span>`);
              } else {
                _push2(`<!---->`);
              }
              _push2(`</span>`);
            } else {
              _push2(`<!---->`);
            }
            _push2(`</p></div><div class="flex items-center gap-2" data-v-d2744000${_scopeId}>`);
            _push2(ssrRenderComponent(_component_UButton, {
              color: "gray",
              variant: "ghost",
              size: "sm",
              loading: unref(loading),
              disabled: unref(loading),
              onClick: reload
            }, {
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(` Refresh `);
                } else {
                  return [
                    createTextVNode(" Refresh ")
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
            _push2(ssrRenderComponent(_component_UButton, {
              color: "red",
              variant: "soft",
              size: "sm",
              disabled: !((_b = unref(data)) == null ? void 0 : _b.exists) || ((_c = unref(data)) == null ? void 0 : _c.total) === 0 || unref(clearing),
              onClick: ($event) => confirmClear.value = true
            }, {
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(` Clear log `);
                } else {
                  return [
                    createTextVNode(" Clear log ")
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
            _push2(`</div></div>`);
          } else {
            return [
              createVNode("div", { class: "flex items-center justify-between gap-3 flex-wrap" }, [
                createVNode("div", null, [
                  createVNode("h1", { class: "text-lg font-semibold" }, "Logs"),
                  createVNode("p", { class: "text-xs text-gray-500 mt-1" }, [
                    createVNode("code", null, toDisplayString(((_d = unref(data)) == null ? void 0 : _d.path) || "\u2014"), 1),
                    unref(data) && !unref(data).exists ? (openBlock(), createBlock("span", {
                      key: 0,
                      class: "ml-2 text-amber-600"
                    }, "not found")) : unref(data) ? (openBlock(), createBlock("span", {
                      key: 1,
                      class: "ml-2 text-gray-500"
                    }, [
                      createTextVNode(toDisplayString(unref(data).total.toLocaleString()) + " line(s) total", 1),
                      unref(data).truncated ? (openBlock(), createBlock("span", { key: 0 }, ", tailing last " + toDisplayString(unref(data).limit.toLocaleString()), 1)) : createCommentVNode("", true)
                    ])) : createCommentVNode("", true)
                  ])
                ]),
                createVNode("div", { class: "flex items-center gap-2" }, [
                  createVNode(_component_UButton, {
                    color: "gray",
                    variant: "ghost",
                    size: "sm",
                    loading: unref(loading),
                    disabled: unref(loading),
                    onClick: reload
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" Refresh ")
                    ]),
                    _: 1
                  }, 8, ["loading", "disabled"]),
                  createVNode(_component_UButton, {
                    color: "red",
                    variant: "soft",
                    size: "sm",
                    disabled: !((_e = unref(data)) == null ? void 0 : _e.exists) || ((_f = unref(data)) == null ? void 0 : _f.total) === 0 || unref(clearing),
                    onClick: ($event) => confirmClear.value = true
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" Clear log ")
                    ]),
                    _: 1
                  }, 8, ["disabled", "onClick"])
                ])
              ])
            ];
          }
        }),
        default: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            if (unref(loadError)) {
              _push2(ssrRenderComponent(_component_UAlert, {
                color: "red",
                variant: "soft",
                title: unref(loadError),
                class: "mb-3"
              }, null, _parent2, _scopeId));
            } else {
              _push2(`<!---->`);
            }
            _push2(`<div class="controls" data-v-d2744000${_scopeId}>`);
            _push2(ssrRenderComponent(_component_UFormGroup, {
              label: "Search",
              class: "search-group"
            }, {
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(ssrRenderComponent(_component_UInput, {
                    modelValue: unref(searchInput),
                    "onUpdate:modelValue": ($event) => isRef(searchInput) ? searchInput.value = $event : null,
                    placeholder: "Case-insensitive substring filter\u2026",
                    icon: "i-heroicons-magnifying-glass",
                    loading: unref(searchInput) !== unref(query)
                  }, null, _parent3, _scopeId2));
                } else {
                  return [
                    createVNode(_component_UInput, {
                      modelValue: unref(searchInput),
                      "onUpdate:modelValue": ($event) => isRef(searchInput) ? searchInput.value = $event : null,
                      placeholder: "Case-insensitive substring filter\u2026",
                      icon: "i-heroicons-magnifying-glass",
                      loading: unref(searchInput) !== unref(query)
                    }, null, 8, ["modelValue", "onUpdate:modelValue", "loading"])
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
            _push2(ssrRenderComponent(_component_UFormGroup, {
              label: "Tail lines",
              class: "tail-group"
            }, {
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(ssrRenderComponent(_component_USelect, {
                    modelValue: unref(limit),
                    "onUpdate:modelValue": ($event) => isRef(limit) ? limit.value = $event : null,
                    modelModifiers: { number: true },
                    options: limitOptions
                  }, null, _parent3, _scopeId2));
                } else {
                  return [
                    createVNode(_component_USelect, {
                      modelValue: unref(limit),
                      "onUpdate:modelValue": ($event) => isRef(limit) ? limit.value = $event : null,
                      modelModifiers: { number: true },
                      options: limitOptions
                    }, null, 8, ["modelValue", "onUpdate:modelValue"])
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
            _push2(`<div class="autopoll" data-v-d2744000${_scopeId}>`);
            _push2(ssrRenderComponent(_component_UCheckbox, {
              modelValue: unref(autoPoll),
              "onUpdate:modelValue": ($event) => isRef(autoPoll) ? autoPoll.value = $event : null,
              label: "Auto-refresh (5s)"
            }, null, _parent2, _scopeId));
            _push2(`</div></div>`);
            if (unref(data)) {
              _push2(`<div class="meta" data-v-d2744000${_scopeId}>`);
              if (unref(query)) {
                _push2(`<span data-v-d2744000${_scopeId}> Showing ${ssrInterpolate(unref(data).shown.toLocaleString())} match(es) in last ${ssrInterpolate(Math.min(unref(data).total, unref(data).limit).toLocaleString())} line(s) </span>`);
              } else {
                _push2(`<span data-v-d2744000${_scopeId}> Showing last ${ssrInterpolate(unref(data).shown.toLocaleString())} line(s) </span>`);
              }
              _push2(`</div>`);
            } else {
              _push2(`<!---->`);
            }
            if (unref(data) && !unref(data).exists) {
              _push2(`<div class="empty" data-v-d2744000${_scopeId}><p class="empty-title" data-v-d2744000${_scopeId}>Log file does not exist yet</p><p class="empty-sub" data-v-d2744000${_scopeId}> aidev creates it on first run. Once <code data-v-d2744000${_scopeId}>${ssrInterpolate(unref(data).path)}</code> exists, lines will appear here. </p></div>`);
            } else if (unref(data) && unref(data).lines.length === 0) {
              _push2(`<div class="empty" data-v-d2744000${_scopeId}><p class="empty-title" data-v-d2744000${_scopeId}>No matching lines</p>`);
              if (unref(query)) {
                _push2(`<p class="empty-sub" data-v-d2744000${_scopeId}> Nothing in the last ${ssrInterpolate(unref(data).limit.toLocaleString())} lines matches \u201C${ssrInterpolate(unref(query))}\u201D. </p>`);
              } else {
                _push2(`<p class="empty-sub" data-v-d2744000${_scopeId}> The log file is empty. </p>`);
              }
              _push2(`</div>`);
            } else if (unref(data)) {
              _push2(`<pre class="viewer" data-v-d2744000${_scopeId}><code data-v-d2744000${_scopeId}>${ssrInterpolate(unref(data).lines.join("\n"))}</code></pre>`);
            } else {
              _push2(`<!---->`);
            }
          } else {
            return [
              unref(loadError) ? (openBlock(), createBlock(_component_UAlert, {
                key: 0,
                color: "red",
                variant: "soft",
                title: unref(loadError),
                class: "mb-3"
              }, null, 8, ["title"])) : createCommentVNode("", true),
              createVNode("div", { class: "controls" }, [
                createVNode(_component_UFormGroup, {
                  label: "Search",
                  class: "search-group"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_UInput, {
                      modelValue: unref(searchInput),
                      "onUpdate:modelValue": ($event) => isRef(searchInput) ? searchInput.value = $event : null,
                      placeholder: "Case-insensitive substring filter\u2026",
                      icon: "i-heroicons-magnifying-glass",
                      loading: unref(searchInput) !== unref(query)
                    }, null, 8, ["modelValue", "onUpdate:modelValue", "loading"])
                  ]),
                  _: 1
                }),
                createVNode(_component_UFormGroup, {
                  label: "Tail lines",
                  class: "tail-group"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_USelect, {
                      modelValue: unref(limit),
                      "onUpdate:modelValue": ($event) => isRef(limit) ? limit.value = $event : null,
                      modelModifiers: { number: true },
                      options: limitOptions
                    }, null, 8, ["modelValue", "onUpdate:modelValue"])
                  ]),
                  _: 1
                }),
                createVNode("div", { class: "autopoll" }, [
                  createVNode(_component_UCheckbox, {
                    modelValue: unref(autoPoll),
                    "onUpdate:modelValue": ($event) => isRef(autoPoll) ? autoPoll.value = $event : null,
                    label: "Auto-refresh (5s)"
                  }, null, 8, ["modelValue", "onUpdate:modelValue"])
                ])
              ]),
              unref(data) ? (openBlock(), createBlock("div", {
                key: 1,
                class: "meta"
              }, [
                unref(query) ? (openBlock(), createBlock("span", { key: 0 }, " Showing " + toDisplayString(unref(data).shown.toLocaleString()) + " match(es) in last " + toDisplayString(Math.min(unref(data).total, unref(data).limit).toLocaleString()) + " line(s) ", 1)) : (openBlock(), createBlock("span", { key: 1 }, " Showing last " + toDisplayString(unref(data).shown.toLocaleString()) + " line(s) ", 1))
              ])) : createCommentVNode("", true),
              unref(data) && !unref(data).exists ? (openBlock(), createBlock("div", {
                key: 2,
                class: "empty"
              }, [
                createVNode("p", { class: "empty-title" }, "Log file does not exist yet"),
                createVNode("p", { class: "empty-sub" }, [
                  createTextVNode(" aidev creates it on first run. Once "),
                  createVNode("code", null, toDisplayString(unref(data).path), 1),
                  createTextVNode(" exists, lines will appear here. ")
                ])
              ])) : unref(data) && unref(data).lines.length === 0 ? (openBlock(), createBlock("div", {
                key: 3,
                class: "empty"
              }, [
                createVNode("p", { class: "empty-title" }, "No matching lines"),
                unref(query) ? (openBlock(), createBlock("p", {
                  key: 0,
                  class: "empty-sub"
                }, " Nothing in the last " + toDisplayString(unref(data).limit.toLocaleString()) + " lines matches \u201C" + toDisplayString(unref(query)) + "\u201D. ", 1)) : (openBlock(), createBlock("p", {
                  key: 1,
                  class: "empty-sub"
                }, " The log file is empty. "))
              ])) : unref(data) ? (openBlock(), createBlock("pre", {
                key: 4,
                ref_key: "viewer",
                ref: viewer,
                class: "viewer"
              }, [
                createVNode("code", null, toDisplayString(unref(data).lines.join("\n")), 1)
              ], 512)) : createCommentVNode("", true)
            ];
          }
        }),
        _: 1
      }, _parent));
      _push(ssrRenderComponent(_component_UModal, {
        modelValue: unref(confirmClear),
        "onUpdate:modelValue": ($event) => isRef(confirmClear) ? confirmClear.value = $event : null
      }, {
        default: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            _push2(ssrRenderComponent(_component_UCard, null, {
              header: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(`<h3 class="text-base font-semibold" data-v-d2744000${_scopeId2}>Clear log file?</h3>`);
                } else {
                  return [
                    createVNode("h3", { class: "text-base font-semibold" }, "Clear log file?")
                  ];
                }
              }),
              footer: withCtx((_2, _push3, _parent3, _scopeId2) => {
                if (_push3) {
                  _push3(`<div class="flex justify-end gap-2" data-v-d2744000${_scopeId2}>`);
                  _push3(ssrRenderComponent(_component_UButton, {
                    variant: "ghost",
                    color: "gray",
                    disabled: unref(clearing),
                    onClick: ($event) => confirmClear.value = false
                  }, {
                    default: withCtx((_3, _push4, _parent4, _scopeId3) => {
                      if (_push4) {
                        _push4(` Cancel `);
                      } else {
                        return [
                          createTextVNode(" Cancel ")
                        ];
                      }
                    }),
                    _: 1
                  }, _parent3, _scopeId2));
                  _push3(ssrRenderComponent(_component_UButton, {
                    color: "red",
                    loading: unref(clearing),
                    onClick: clearLog
                  }, {
                    default: withCtx((_3, _push4, _parent4, _scopeId3) => {
                      if (_push4) {
                        _push4(` Clear log `);
                      } else {
                        return [
                          createTextVNode(" Clear log ")
                        ];
                      }
                    }),
                    _: 1
                  }, _parent3, _scopeId2));
                  _push3(`</div>`);
                } else {
                  return [
                    createVNode("div", { class: "flex justify-end gap-2" }, [
                      createVNode(_component_UButton, {
                        variant: "ghost",
                        color: "gray",
                        disabled: unref(clearing),
                        onClick: ($event) => confirmClear.value = false
                      }, {
                        default: withCtx(() => [
                          createTextVNode(" Cancel ")
                        ]),
                        _: 1
                      }, 8, ["disabled", "onClick"]),
                      createVNode(_component_UButton, {
                        color: "red",
                        loading: unref(clearing),
                        onClick: clearLog
                      }, {
                        default: withCtx(() => [
                          createTextVNode(" Clear log ")
                        ]),
                        _: 1
                      }, 8, ["loading"])
                    ])
                  ];
                }
              }),
              default: withCtx((_2, _push3, _parent3, _scopeId2) => {
                var _a, _b;
                if (_push3) {
                  _push3(`<p class="text-sm text-gray-600" data-v-d2744000${_scopeId2}> This truncates <code data-v-d2744000${_scopeId2}>${ssrInterpolate((_a = unref(data)) == null ? void 0 : _a.path)}</code> to zero bytes. The file itself stays in place. This cannot be undone. </p>`);
                } else {
                  return [
                    createVNode("p", { class: "text-sm text-gray-600" }, [
                      createTextVNode(" This truncates "),
                      createVNode("code", null, toDisplayString((_b = unref(data)) == null ? void 0 : _b.path), 1),
                      createTextVNode(" to zero bytes. The file itself stays in place. This cannot be undone. ")
                    ])
                  ];
                }
              }),
              _: 1
            }, _parent2, _scopeId));
          } else {
            return [
              createVNode(_component_UCard, null, {
                header: withCtx(() => [
                  createVNode("h3", { class: "text-base font-semibold" }, "Clear log file?")
                ]),
                footer: withCtx(() => [
                  createVNode("div", { class: "flex justify-end gap-2" }, [
                    createVNode(_component_UButton, {
                      variant: "ghost",
                      color: "gray",
                      disabled: unref(clearing),
                      onClick: ($event) => confirmClear.value = false
                    }, {
                      default: withCtx(() => [
                        createTextVNode(" Cancel ")
                      ]),
                      _: 1
                    }, 8, ["disabled", "onClick"]),
                    createVNode(_component_UButton, {
                      color: "red",
                      loading: unref(clearing),
                      onClick: clearLog
                    }, {
                      default: withCtx(() => [
                        createTextVNode(" Clear log ")
                      ]),
                      _: 1
                    }, 8, ["loading"])
                  ])
                ]),
                default: withCtx(() => {
                  var _a;
                  return [
                    createVNode("p", { class: "text-sm text-gray-600" }, [
                      createTextVNode(" This truncates "),
                      createVNode("code", null, toDisplayString((_a = unref(data)) == null ? void 0 : _a.path), 1),
                      createTextVNode(" to zero bytes. The file itself stays in place. This cannot be undone. ")
                    ])
                  ];
                }),
                _: 1
              })
            ];
          }
        }),
        _: 1
      }, _parent));
      _push(`</div>`);
    };
  }
});
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
  const ssrContext = useSSRContext();
  (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("pages/logs.vue");
  return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
const logs = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-d2744000"]]);

export { logs as default };
//# sourceMappingURL=logs-hZKOhIqF.mjs.map
