"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const [name, bem] = create.createNamespace("highlight");
const highlightProps = {
  autoEscape: props.truthProp,
  caseSensitive: Boolean,
  highlightClass: String,
  highlightTag: props.makeStringProp("span"),
  keywords: props.makeRequiredProp([String, Array]),
  sourceString: props.makeStringProp(""),
  tag: props.makeStringProp("div"),
  unhighlightClass: String,
  unhighlightTag: props.makeStringProp("span")
};
var stdin_default = Vue.defineComponent({
  name,
  props: highlightProps,
  setup(props2) {
    const highlightChunks = Vue.computed(() => {
      const {
        autoEscape,
        caseSensitive,
        keywords,
        sourceString
      } = props2;
      const flags = caseSensitive ? "g" : "gi";
      const _keywords = Array.isArray(keywords) ? keywords : [keywords];
      let chunks = _keywords.filter((keyword) => keyword).reduce((chunks2, keyword) => {
        if (autoEscape) {
          keyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
        }
        const regex = new RegExp(keyword, flags);
        let match;
        while (match = regex.exec(sourceString)) {
          const start = match.index;
          const end = regex.lastIndex;
          if (start >= end) {
            regex.lastIndex++;
            continue;
          }
          chunks2.push({
            start,
            end,
            highlight: true
          });
        }
        return chunks2;
      }, []);
      chunks = chunks.sort((a, b) => a.start - b.start).reduce((chunks2, currentChunk) => {
        const prevChunk = chunks2[chunks2.length - 1];
        if (!prevChunk || currentChunk.start > prevChunk.end) {
          const unhighlightStart = prevChunk ? prevChunk.end : 0;
          const unhighlightEnd = currentChunk.start;
          if (unhighlightStart !== unhighlightEnd) {
            chunks2.push({
              start: unhighlightStart,
              end: unhighlightEnd,
              highlight: false
            });
          }
          chunks2.push(currentChunk);
        } else {
          prevChunk.end = Math.max(prevChunk.end, currentChunk.end);
        }
        return chunks2;
      }, []);
      const lastChunk = chunks[chunks.length - 1];
      if (!lastChunk) {
        chunks.push({
          start: 0,
          end: sourceString.length,
          highlight: false
        });
      }
      if (lastChunk && lastChunk.end < sourceString.length) {
        chunks.push({
          start: lastChunk.end,
          end: sourceString.length,
          highlight: false
        });
      }
      return chunks;
    });
    const renderContent = () => {
      const {
        sourceString,
        highlightClass,
        unhighlightClass,
        highlightTag,
        unhighlightTag
      } = props2;
      return highlightChunks.value.map((chunk) => {
        const {
          start,
          end,
          highlight
        } = chunk;
        const text = sourceString.slice(start, end);
        if (highlight) {
          return Vue.createVNode(highlightTag, {
            "class": [bem("tag"), highlightClass]
          }, {
            default: () => [text]
          });
        }
        return Vue.createVNode(unhighlightTag, {
          "class": unhighlightClass
        }, {
          default: () => [text]
        });
      });
    };
    return () => {
      const {
        tag
      } = props2;
      return Vue.createVNode(tag, {
        "class": bem()
      }, {
        default: () => [renderContent()]
      });
    };
  }
});
exports.default = stdin_default;
exports.highlightProps = highlightProps;
