import { computed as m, onMounted as ze, ref as b, watch as Ye, openBlock as i, createElementBlock as c, normalizeClass as ge, normalizeStyle as x, unref as u, createBlock as O, createCommentVNode as C, createSlots as De, withCtx as y, renderSlot as k, normalizeProps as X, guardReactiveProps as W, createVNode as be, createElementVNode as M, Fragment as P, renderList as F, toDisplayString as q, createTextVNode as Ge, nextTick as Ue } from "vue";
import { u as Ve, o as Be, e as je, c as ye, t as Xe, a as We, p as R, b as qe, v as f, f as ke, X as Re, G as we, L as Ze, i as Je, q as Ke, r as Qe } from "./index-BbPeL8Gp.js";
import { _ as et } from "./Title-aSAft5dl.js";
import { u as tt, U as ot } from "./usePrinter-DaybDVqP.js";
import { D as lt } from "./DataTable-DGf6RrOJ.js";
import at from "./vue-ui-accordion-5d5iVpr7.js";
import nt from "./vue-ui-skeleton-Bniq21d9.js";
import { u as Ce } from "./useNestedProp-BN8claGG.js";
import { _ as st } from "./PackageVersion-CxJghxYD.js";
import { P as rt } from "./PenAndPaper-ZdZEumki.js";
import { u as ut } from "./useUserOptionState-BIvW1Kz7.js";
import { _ as it } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const ct = ["id"], dt = {
  key: 1,
  ref: "noTitle",
  class: "vue-data-ui-no-title-space",
  style: "height:36px; width: 100%;background:transparent"
}, vt = {
  key: 2,
  style: "width:100%;background:transparent;padding-bottom:24px"
}, ft = ["xmlns", "viewBox"], ht = ["id"], mt = ["stop-color"], pt = ["stop-color"], gt = ["d", "fill", "stroke", "stroke-width"], bt = ["x", "y", "height", "width", "fill", "stroke", "stroke-width", "onMouseenter"], yt = ["x", "y", "font-size", "fill"], kt = ["x", "y", "font-size", "fill"], wt = {
  key: 4,
  class: "vue-data-ui-watermark"
}, Ct = {
  key: 6,
  ref: "source",
  dir: "auto"
}, $t = ["innerHTML"], xt = {
  __name: "vue-ui-flow",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup($e, { expose: xe }) {
    const p = $e, { vue_ui_flow: _e } = Ve(), E = m(() => !!p.dataset && p.dataset.length);
    ze(() => {
      Z();
    });
    function Z() {
      Be(p.dataset) && je({
        componentName: "VueUiFlow",
        type: "dataset"
      });
    }
    const z = b(ye()), Y = b(null), J = b(0), K = b(0), T = b(!1);
    function Ne(t) {
      T.value = t, J.value += 1;
    }
    const e = m({
      get: () => oe(),
      set: (t) => t
    }), { userOptionsVisible: Q, setUserOptionsVisibility: ee, keepUserOptionState: te } = ut({ config: e.value });
    function oe() {
      const t = Ce({
        userConfig: p.config,
        defaultConfig: _e
      });
      return t.theme ? {
        ...Ce({
          userConfig: Xe.vue_ui_flow[t.theme] || p.config,
          defaultConfig: t
        }),
        customPalette: We[t.theme] || R
      } : t;
    }
    Ye(() => p.config, (t) => {
      e.value = oe(), Z(), K.value += 1;
    }, { deep: !0 });
    const { isPrinting: le, isImaging: ae, generatePdf: ne, generateImage: se } = tt({
      elementId: `flow_${z.value}`,
      fileName: e.value.style.chart.title.text || "vue-ui-flow"
    }), Te = m(() => e.value.userOptions.show && !e.value.style.chart.title.text), D = m(() => qe(e.value.customPalette)), re = m(() => e.value.style.chart.nodes.gap), _ = m(() => e.value.style.chart.nodes.width), I = b({
      showTable: e.value.table.show
    }), ue = m(() => e.value.style.chart.links.width), G = m(() => !p.dataset || !p.dataset.length ? [] : p.dataset.map((t) => [
      t[0],
      t[1],
      f(t[2])
    ])), ie = m(() => {
      const t = {};
      function l(n) {
        t[n] || (t[n] = { inflow: 0, outflow: 0 });
      }
      G.value.forEach(([n, a, v]) => {
        l(n), l(a), t[n].outflow += v, t[a].inflow += v;
      });
      let o = 0;
      for (const n in t) {
        const a = Math.max(t[n].inflow, t[n].outflow);
        o = Math.max(o, a);
      }
      return o;
    });
    function Oe(t) {
      return t / ie.value * 100 + e.value.style.chart.nodes.minHeight;
    }
    function Pe(t) {
      const l = e.value.style.chart.nodes.minHeight;
      return (t - l) / 100 * ie.value;
    }
    function Fe(t) {
      const l = {}, o = {};
      function n(s, r) {
        l[s] || (l[s] = { height: 0, level: null, inflow: 0, outflow: 0 }), l[s].level === null && (l[s].level = r), o[r] || (o[r] = []), o[r].includes(s) || o[r].push(s);
      }
      t.forEach(([s, r, h], N) => {
        const $ = l[s] ? l[s].level : 0, S = $ + 1;
        n(s, $), n(r, S), l[s].children || (l[s].children = []), l[s].children.push({ target: r, value: h }), l[s].outflow += h, l[r].inflow += h;
      }), Object.keys(l).forEach((s, r) => {
        l[s].color = D.value[r] || D.value[r % D.value.length] || R[r] || R[r % d.length];
      });
      for (const s in l)
        l[s].height = Oe(Math.max(l[s].inflow, l[s].outflow)), l[s].name = s;
      const a = {};
      for (const s in o) {
        let r = 0;
        o[s].forEach((h, N) => {
          const $ = l[h].height;
          a[h] = {
            x: parseInt(s, 10) * ue.value + e.value.style.chart.padding.left,
            y: r,
            absoluteY: r,
            height: $,
            i: N,
            color: l[h].color,
            value: Pe($)
          }, r += $ + re.value;
        });
      }
      const v = [];
      for (const s in l) {
        let r = a[s].absoluteY;
        l[s].children && l[s].children.forEach(({ target: h, value: N }, $) => {
          const S = a[h].y, B = a[s], j = a[h], fe = f(r), he = f(r + N / l[s].outflow * B.height), me = f(S), pe = f(S + N / l[h].inflow * j.height), Ee = {
            id: ye(),
            source: s,
            target: h,
            path: `M ${f(B.x) + f(_.value)} ${fe} L ${f(B.x) + f(_.value)} ${he} L ${f(j.x)} ${pe} L ${f(j.x)} ${me} Z`,
            value: N,
            sourceColor: l[s].color,
            targetColor: l[h].color
          };
          v.push(Ee), r += he - fe, a[h].y += pe - me;
        });
      }
      return { nodeCoordinates: a, links: v };
    }
    const g = m(() => {
      const t = Fe(p.dataset);
      return {
        nodes: Object.keys(t.nodeCoordinates).map((l, o) => ({
          ...t.nodeCoordinates[l],
          name: l
        })),
        links: t.links
      };
    }), Ie = m(() => Le(g.value.nodes));
    function Le(t) {
      const l = {};
      for (const n in t) {
        const { x: a, height: v } = t[n];
        l[a] || (l[a] = 0), l[a] += v + re.value;
      }
      return Math.max(...Object.values(l));
    }
    const U = m(() => {
      const { top: t, right: l, left: o, bottom: n } = e.value.style.chart.padding, a = G.value.length * ue.value;
      return {
        height: Ie.value + t + n,
        width: l + Math.max(...g.value.nodes.map((v) => v.x)) + _.value,
        left: o,
        top: t,
        right: a - l,
        p_top: t,
        p_bottom: n
      };
    });
    function Ae(t) {
      const l = {}, o = {}, n = /* @__PURE__ */ new Set();
      return G.value.forEach(([a, v, s]) => {
        l[a] || (l[a] = []), o[v] || (o[v] = []), l[a].push(v), o[v].push(a);
      }), l[t] && l[t].forEach((a) => n.add(a)), o[t] && o[t].forEach((a) => n.add(a)), Array.from(n).concat(t);
    }
    const w = b(null), L = b(null);
    function He(t) {
      w.value = Ae(t.name), L.value = t.name;
    }
    function Se() {
      w.value = null, L.value = null;
    }
    const ce = m(() => g.value.links.map(({ source: t, target: l, sourceColor: o, targetColor: n, value: a }) => ({
      source: t,
      target: l,
      sourceColor: o,
      targetColor: n,
      value: a
    })));
    function de() {
      Ue(() => {
        const t = ce.value.map((n, a) => [
          [n.source],
          [n.target],
          [n.value]
        ]), l = [
          [e.value.style.chart.title.text],
          [e.value.style.chart.title.subtitle.text],
          [
            [e.value.table.columnNames.source],
            [e.value.table.columnNames.target],
            [e.value.table.columnNames.value]
          ]
        ].concat(t), o = Ke(l);
        Qe({
          csvContent: o,
          title: e.value.style.chart.title.text || "vue-ui-flow"
        });
      });
    }
    const A = m(() => {
      const t = [
        e.value.table.columnNames.source,
        e.value.table.columnNames.target,
        e.value.table.columnNames.value
      ], l = ce.value.map((a, v) => [
        {
          color: a.sourceColor,
          name: a.source
        },
        {
          color: a.targetColor,
          name: a.target
        },
        ke({
          p: e.value.style.chart.nodes.labels.prefix,
          v: a.value,
          s: e.value.style.chart.nodes.labels.suffix,
          r: e.value.style.chart.nodes.labels.rounding
        })
      ]), o = {
        th: {
          backgroundColor: e.value.table.th.backgroundColor,
          color: e.value.table.th.color,
          outline: e.value.table.th.outline
        },
        td: {
          backgroundColor: e.value.table.td.backgroundColor,
          color: e.value.table.td.color,
          outline: e.value.table.td.outline
        },
        breakpoint: e.value.table.responsiveBreakpoint
      };
      return {
        colNames: [
          e.value.table.columnNames.source,
          e.value.table.columnNames.target,
          e.value.table.columnNames.value
        ],
        head: t,
        body: l,
        config: o
      };
    });
    function Me() {
      return g.value;
    }
    function ve() {
      I.value.showTable = !I.value.showTable;
    }
    const H = b(!1);
    function V() {
      H.value = !H.value;
    }
    return xe({
      getData: Me,
      generateCsv: de,
      generateImage: se,
      generatePdf: ne,
      toggleTable: ve,
      toggleAnnotator: V
    }), (t, l) => (i(), c("div", {
      ref_key: "flowChart",
      ref: Y,
      class: ge(`vue-ui-flow ${T.value ? "vue-data-ui-wrapper-fullscreen" : ""}`),
      style: x(`font-family:${e.value.style.fontFamily};width:100%; text-align:center;background:${e.value.style.chart.backgroundColor}`),
      id: `flow_${z.value}`,
      onMouseenter: l[2] || (l[2] = () => u(ee)(!0)),
      onMouseleave: l[3] || (l[3] = () => u(ee)(!1))
    }, [
      e.value.userOptions.buttons.annotator ? (i(), O(rt, {
        key: 0,
        parent: Y.value,
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        active: H.value,
        onClose: V
      }, null, 8, ["parent", "backgroundColor", "color", "active"])) : C("", !0),
      Te.value ? (i(), c("div", dt, null, 512)) : C("", !0),
      e.value.style.chart.title.text ? (i(), c("div", vt, [
        (i(), O(et, {
          key: `title_${K.value}`,
          config: {
            title: {
              cy: "flow-title",
              ...e.value.style.chart.title
            },
            subtitle: {
              cy: "flow-subtitle",
              ...e.value.style.chart.title.subtitle
            }
          }
        }, null, 8, ["config"]))
      ])) : C("", !0),
      e.value.userOptions.show && E.value && (u(te) || u(Q)) ? (i(), O(ot, {
        ref: "details",
        key: `user_option_${J.value}`,
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        isPrinting: u(le),
        isImaging: u(ae),
        uid: z.value,
        hasPdf: e.value.userOptions.buttons.pdf,
        hasXls: e.value.userOptions.buttons.csv,
        hasImg: e.value.userOptions.buttons.img,
        hasTable: e.value.userOptions.buttons.table,
        hasFullscreen: e.value.userOptions.buttons.fullscreen,
        isFullscreen: T.value,
        titles: { ...e.value.userOptions.buttonTitles },
        chartElement: Y.value,
        position: e.value.userOptions.position,
        hasAnnotator: e.value.userOptions.buttons.annotator,
        isAnnotation: H.value,
        onToggleFullscreen: Ne,
        onGeneratePdf: u(ne),
        onGenerateCsv: de,
        onGenerateImage: u(se),
        onToggleTable: ve,
        onToggleAnnotator: V,
        style: x({
          visibility: u(te) ? u(Q) ? "visible" : "hidden" : "visible"
        })
      }, De({ _: 2 }, [
        t.$slots.optionPdf ? {
          name: "optionPdf",
          fn: y(() => [
            k(t.$slots, "optionPdf", {}, void 0, !0)
          ]),
          key: "0"
        } : void 0,
        t.$slots.optionCsv ? {
          name: "optionCsv",
          fn: y(() => [
            k(t.$slots, "optionCsv", {}, void 0, !0)
          ]),
          key: "1"
        } : void 0,
        t.$slots.optionImg ? {
          name: "optionImg",
          fn: y(() => [
            k(t.$slots, "optionImg", {}, void 0, !0)
          ]),
          key: "2"
        } : void 0,
        t.$slots.optionTable ? {
          name: "optionTable",
          fn: y(() => [
            k(t.$slots, "optionTable", {}, void 0, !0)
          ]),
          key: "3"
        } : void 0,
        t.$slots.optionFullscreen ? {
          name: "optionFullscreen",
          fn: y(({ toggleFullscreen: o, isFullscreen: n }) => [
            k(t.$slots, "optionFullscreen", X(W({ toggleFullscreen: o, isFullscreen: n })), void 0, !0)
          ]),
          key: "4"
        } : void 0,
        t.$slots.optionAnnotator ? {
          name: "optionAnnotator",
          fn: y(({ toggleAnnotator: o, isAnnotator: n }) => [
            k(t.$slots, "optionAnnotator", X(W({ toggleAnnotator: o, isAnnotator: n })), void 0, !0)
          ]),
          key: "5"
        } : void 0
      ]), 1032, ["backgroundColor", "color", "isPrinting", "isImaging", "uid", "hasPdf", "hasXls", "hasImg", "hasTable", "hasFullscreen", "isFullscreen", "titles", "chartElement", "position", "hasAnnotator", "isAnnotation", "onGeneratePdf", "onGenerateImage", "style"])) : C("", !0),
      (i(), c("svg", {
        xmlns: u(Re),
        viewBox: `0 0 ${U.value.width} ${U.value.height}`,
        class: ge({ "vue-data-ui-fullscreen--on": T.value, "vue-data-ui-fulscreen--off": !T.value }),
        style: x(`max-width:100%; overflow: visible; background:transparent;color:${e.value.style.chart.color}`)
      }, [
        be(st),
        M("defs", null, [
          (i(!0), c(P, null, F(g.value.links, (o, n) => (i(), c("linearGradient", {
            id: o.id,
            x1: "0%",
            y1: "0%",
            x2: "100%",
            y2: "0%"
          }, [
            M("stop", {
              offset: "0%",
              "stop-color": o.sourceColor
            }, null, 8, mt),
            M("stop", {
              offset: "100%",
              "stop-color": o.targetColor
            }, null, 8, pt)
          ], 8, ht))), 256))
        ]),
        (i(!0), c(P, null, F(g.value.links, (o) => (i(), c("path", {
          class: "vue-ui-flow-link",
          d: o.path,
          fill: `url(#${o.id})`,
          stroke: e.value.style.chart.links.stroke,
          "stroke-width": e.value.style.chart.links.strokeWidth,
          style: x(`opacity:${L.value ? [o.target, o.source].includes(L.value) ? 1 : 0.3 : e.value.style.chart.links.opacity}`)
        }, null, 12, gt))), 256)),
        (i(!0), c(P, null, F(g.value.nodes, (o, n) => (i(), c("rect", {
          class: "vue-ui-flow-node",
          x: o.x,
          y: u(f)(o.absoluteY),
          height: u(f)(o.height),
          width: _.value,
          fill: o.color,
          stroke: e.value.style.chart.nodes.stroke,
          "stroke-width": e.value.style.chart.nodes.strokeWidth,
          onMouseenter: (a) => He(o),
          onMouseleave: l[0] || (l[0] = (a) => Se()),
          style: x(`opacity:${w.value ? w.value.includes(o.name) ? 1 : 0.2 : 1}`)
        }, null, 44, bt))), 256)),
        (i(!0), c(P, null, F(g.value.nodes, (o, n) => (i(), c("text", {
          x: o.x + _.value / 2,
          y: u(f)(o.absoluteY + o.height / 2 - e.value.style.chart.nodes.labels.fontSize / 4),
          "font-size": e.value.style.chart.nodes.labels.fontSize,
          fill: u(we)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${w.value ? w.value.includes(o.name) ? 1 : 0 : 1}`)
        }, q(e.value.style.chart.nodes.labels.abbreviation.use ? u(Ze)({ source: o.name, length: e.value.style.chart.nodes.labels.abbreviation.length }) : o.name), 13, yt))), 256)),
        (i(!0), c(P, null, F(g.value.nodes, (o, n) => (i(), c("text", {
          x: o.x + _.value / 2,
          y: u(f)(o.absoluteY + o.height / 2 + e.value.style.chart.nodes.labels.fontSize),
          "font-size": e.value.style.chart.nodes.labels.fontSize,
          fill: u(we)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${w.value ? w.value.includes(o.name) ? 1 : 0 : 1}`)
        }, q(u(Je)(
          e.value.style.chart.nodes.labels.formatter,
          o.value,
          u(ke)({
            p: e.value.style.chart.nodes.labels.prefix,
            v: o.value,
            s: e.value.style.chart.nodes.labels.suffix,
            r: e.value.style.chart.nodes.labels.rounding
          }),
          { datapoint: o, seriesIndex: n }
        )), 13, kt))), 256)),
        k(t.$slots, "svg", { svg: U.value }, void 0, !0)
      ], 14, ft)),
      t.$slots.watermark ? (i(), c("div", wt, [
        k(t.$slots, "watermark", X(W({ isPrinting: u(le) || u(ae) })), void 0, !0)
      ])) : C("", !0),
      E.value ? C("", !0) : (i(), O(nt, {
        key: 5,
        config: {
          type: "flow",
          style: {
            backgroundColor: e.value.style.chart.backgroundColor
          }
        }
      }, null, 8, ["config"])),
      t.$slots.source ? (i(), c("div", Ct, [
        k(t.$slots, "source", {}, void 0, !0)
      ], 512)) : C("", !0),
      E.value ? (i(), O(at, {
        key: 7,
        hideDetails: "",
        config: {
          open: I.value.showTable,
          maxHeight: 1e4,
          body: {
            backgroundColor: e.value.style.chart.backgroundColor,
            color: e.value.style.chart.color
          },
          head: {
            backgroundColor: e.value.style.chart.backgroundColor,
            color: e.value.style.chart.color
          }
        }
      }, {
        content: y(() => [
          be(lt, {
            colNames: A.value.colNames,
            head: A.value.head,
            body: A.value.body,
            config: A.value.config,
            title: `${e.value.style.chart.title.text}${e.value.style.chart.title.subtitle.text ? ` : ${e.value.style.chart.title.subtitle.text}` : ""}`,
            onClose: l[1] || (l[1] = (o) => I.value.showTable = !1)
          }, {
            th: y(({ th: o }) => [
              M("div", {
                innerHTML: o,
                style: { display: "flex", "align-items": "center" }
              }, null, 8, $t)
            ]),
            td: y(({ td: o }) => [
              Ge(q(o.name || o), 1)
            ]),
            _: 1
          }, 8, ["colNames", "head", "body", "config", "title"])
        ]),
        _: 1
      }, 8, ["config"])) : C("", !0)
    ], 46, ct));
  }
}, Et = /* @__PURE__ */ it(xt, [["__scopeId", "data-v-e695afca"]]);
export {
  Et as default
};
