<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<template>
  <div class="log-item flex-h">
    <div
      v-for="(item, index) in columns"
      :key="index"
      :class="item.label"
      @click="selectLog(item.label, getDataValue(item.label))"
    >
      <span v-if="item.label === 'tags'" :class="level.toLowerCase()"> > </span>
      <span class="blue" v-else-if="item.label === 'traceId'">
        <el-tooltip content="Trace Link" v-if="!noLink && getDataValue(item.label)">
          <Icon iconName="merge" />
        </el-tooltip>
      </span>
      <span v-else>
        <template v-for="(part, partIndex) in highlightKeywords(getDataValue(item.label))" :key="partIndex">
          <span v-if="part.highlight" class="keyword-highlight">{{ part.text }}</span>
          <template v-else>{{ part.text }}</template>
        </template>
      </span>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { computed, inject } from "vue";
  import type { PropType } from "vue";
  import { ServiceLogConstants } from "./data";
  import getDashboard from "@/hooks/useDashboardsSession";
  import { useDashboardStore } from "@/store/modules/dashboard";
  import type { LayoutConfig, DashboardItem } from "@/types/dashboard";
  import { WidgetType } from "@/views/dashboard/data";
  import { useLogStore } from "@/store/modules/log";
  import type { LogItem } from "@/types/log";

  /*global defineProps, defineEmits */
  const props = defineProps({
    data: { type: Object as PropType<LogItem>, default: () => ({}) },
    noLink: { type: Boolean, default: true },
    config: { type: Object as PropType<LayoutConfig>, default: () => ({}) },
  });
  const logStore = useLogStore();
  const dashboardStore = useDashboardStore();
  const options: LayoutConfig | null = inject("options") || null;
  const emit = defineEmits(["select"]);
  const columns = ServiceLogConstants;
  const level = computed(() => {
    if (!props.data.tags) {
      return "";
    }
    return (props.data.tags.find((d: { key: string; value: string }) => d.key === "level") || {}).value || "";
  });
  type HighlightPart = {
    text: string;
    highlight: boolean;
  };

  const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

  const highlightKeywords = (content: string): HighlightPart[] => {
    const text = `${content || ""}`;
    const keywords = [
      ...new Set(Object.values(logStore.conditions.keywordsOfContent || {}).map((keyword) => `${keyword}`.trim())),
    ].filter(Boolean);

    if (!keywords.length) {
      return [{ text, highlight: false }];
    }

    const regex = new RegExp(keywords.map(escapeRegExp).join("|"), "gi");
    const parts: HighlightPart[] = [];
    let lastIndex = 0;
    let match: RegExpExecArray | null;

    while ((match = regex.exec(text)) !== null) {
      if (match.index > lastIndex) {
        parts.push({ text: text.slice(lastIndex, match.index), highlight: false });
      }
      parts.push({ text: match[0], highlight: true });
      lastIndex = regex.lastIndex;
    }

    if (lastIndex < text.length) {
      parts.push({ text: text.slice(lastIndex), highlight: false });
    }

    return parts.length ? parts : [{ text, highlight: false }];
  };

  function getDataValue(label: string) {
    return props.data[label as keyof LogItem] as string;
  }

  function selectLog(label: string, value: string) {
    if (label === "traceId") {
      if (!value) {
        emit("select", props.data);
        return;
      }
      linkTrace(value);
      return;
    }
    emit("select", props.data);
  }
  function linkTrace(id: string) {
    const { associationWidget } = getDashboard(dashboardStore.currentDashboard as DashboardItem);
    associationWidget(
      (options?.id || "") as string,
      {
        sourceId: (options?.id || "") as string,
        traceId: id,
        id: props.data.serviceId || "",
      },
      WidgetType.Trace,
    );
  }
</script>
<style lang="scss" scoped>
  .log-item {
    cursor: pointer;
    align-items: center;
    min-height: 30px;

    .traceId {
      cursor: pointer;
      width: 20px;
      text-align: center;
      position: relative;

      span {
        display: inline-block;
        width: 100%;
      }

      .blue {
        color: var(--el-color-primary);
      }
    }

    .tags {
      width: 15px;
      text-align: center;
      color: var(--sw-green);
      font-weight: 400;
      font-size: 14px;
    }

    .content {
      width: 1300px;
    }

    .serviceName {
      width: 200px;
    }
  }

  .log-item:hover {
    background: var(--sw-list-hover);
  }

  .log-item > div {
    width: 60px;
    padding: 0 5px;
    display: inline-block;
    border: 1px solid transparent;
    white-space: normal;
    word-break: break-all;
    overflow: hidden;
  }

  .log-item .text {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .log-item > div.method {
    height: 100%;
    padding: 3px 8px;
  }

  .error {
    color: var(--sw-orange);
  }

  .warning {
    color: var(--sw-orange);
  }

  .keyword-highlight {
    color: var(--sw-red);
  }
</style>
