import { defineComponent as w, ref as A, shallowRef as D, useSlots as W, computed as $, createVNode as u, mergeProps as E, createTextVNode as m, Fragment as P, isVNode as z } from "vue";
import { useNamespace as q, ElSpace as G, ElButton as p, ElPopconfirm as H } from "element-plus";
import { wetEditTableProps as J } from "./types.mjs";
import { WetSchemaTable as K } from "../../../pro-table/index.mjs";
import L from "./columns-item.vue.mjs";
import F from "../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/cloneDeep.mjs";
function M(t) {
  return typeof t == "function" || Object.prototype.toString.call(t) === "[object Object]" && !z(t);
}
const le = /* @__PURE__ */ w({
  name: "WetEditableTabl",
  props: {
    ...J
  },
  emits: ["update:modelValue", "change"],
  setup(t, {
    emit: o,
    expose: S
  }) {
    const V = q("editable-table"), r = A([]), C = D(), d = W(), B = (l, e) => {
      const a = [...t.modelValue || []];
      o("update:modelValue", a), t.controlled || o("change", {
        row: l,
        $index: e
      });
    }, I = (l, e) => {
      o("change", {
        row: l,
        $index: e
      });
    }, c = (l = !1) => {
      const e = t.modelValue || [], a = F(t.addDefaultRow);
      let n = e.length;
      t.controlled && r.value.push(a), l ? (e.unshift(a), n = 0) : e.push(a), o("update:modelValue", e), o("change", {
        row: a,
        $index: n
      });
    }, b = (l) => {
      r.value = r.value.filter((e) => e !== l);
    }, i = (l) => {
      if (t.modelValue && t.modelValue.length > l) {
        const e = [...t.modelValue], a = e.splice(l, 1);
        b(a), o("update:modelValue", e), o("change", {
          row: null,
          $index: l
        });
      }
    }, f = (l, e) => {
      const a = [...t.modelValue || []], n = F(l);
      e !== void 0 ? a.splice(e + 1, 0, n) : a.push(n), o("update:modelValue", a), o("change", {
        row: n,
        $index: e || a.length
      });
    }, h = (l) => {
      r.value.includes(l) || r.value.push(l);
    }, y = D(!1), g = (l) => {
      y.value || (b(l), y.value = !0, setTimeout(() => {
        y.value = !1;
      }, 200));
    }, v = (l) => b(l), _ = (l, e) => {
      var n;
      const a = (n = t.modelValue) == null ? void 0 : n[l];
      a && (Object.assign(a, e), o("update:modelValue", t.modelValue), o("change", {
        row: a,
        $index: l
      }));
    }, j = {
      placement: "top"
    }, x = (l, e, a) => {
      let n;
      return u(G, {
        size: l.length === 0 ? 0 : void 0
      }, M(n = l.map((s) => s === "add" ? u(p, {
        link: !0,
        type: "primary",
        onClick: () => c()
      }, {
        default: () => [m("新增")]
      }) : s === "delete" ? u(H, E(j, {
        title: "删除此项？",
        onConfirm: () => i(a)
      }), {
        reference() {
          return u(p, {
            link: !0,
            type: "danger"
          }, {
            default: () => [m("删除")]
          });
        }
      }) : s === "copy" ? u(p, {
        link: !0,
        type: "primary",
        onClick: () => f(e, a)
      }, {
        default: () => [m("复制")]
      }) : s === "edit" ? u(P, null, [r.value.includes(e) ? u(P, null, [u(p, {
        link: !0,
        type: "primary",
        onClick: () => g(e)
      }, {
        default: () => [m("保存")]
      }), u(p, {
        link: !0,
        type: "primary",
        onClick: () => v(e)
      }, {
        default: () => [m("取消")]
      })]) : u(p, {
        link: !0,
        type: "primary",
        onClick: () => h(e)
      }, {
        default: () => [m("编辑")]
      })]) : void 0)) ? n : {
        default: () => [n]
      });
    }, N = (l) => t.readonly ? !0 : r.value.includes(l) ? !1 : t.controlled, O = $(() => (t.columns || []).filter((e) => t.readonly ? e.valueType !== "actions" : e).map((e) => ({
      ...e,
      isRender: !1,
      render({
        row: a,
        $index: n
      }) {
        var s, k;
        if (e.valueType === "actions")
          return e.isRender ? d.actions && d.actions({
            row: a,
            $index: n,
            actions: {
              add: c,
              deleteRow: i,
              saveRow: g,
              copy: f,
              cancel: v,
              edit: h,
              readonly: !r.value.includes(a)
            }
          }) : e.render ? e.render({
            row: a,
            $index: n
          }, {
            add: c,
            deleteRow: i,
            saveRow: g,
            copy: f,
            cancel: v,
            edit: h,
            readonly: r.value.includes(a)
          }) : x(e.actions || [], a, n);
        {
          const T = Array.isArray(e.dataIndex) ? e.dataIndex.join("") : e.dataIndex;
          let R = a && e.render;
          return e.isRender && a && (R = d[T] && d[T]), u(L, {
            row: a,
            index: n,
            prop: t.prop,
            itemConfig: {
              ...e,
              fieldProps: {
                ...e.fieldProps || {},
                disabled: t.disabled || ((s = e.fieldProps) == null ? void 0 : s.disabled),
                clearable: t.clearable || ((k = e.fieldProps) == null ? void 0 : k.clearable)
              }
            },
            changeValue: B,
            emitControlChangeValue: I,
            readonly: N(a),
            controlled: t.controlled,
            saveTag: y.value
          }, {
            default: R ? () => R({
              $index: n,
              row: a
            }) : null
          });
        }
      }
    })));
    return S({
      scrollToIndex: (l) => {
        var e;
        return (e = C.value) == null ? void 0 : e.scrollToIndex(l);
      },
      add: c,
      deleteRow: i,
      saveRow: g,
      copy: f,
      cancel: v,
      edit: h,
      setRowData: _
    }), () => u("div", {
      class: V.b()
    }, [d.header && u("div", {
      class: V.e("header")
    }, [d.header({
      addBefore: () => c(!0),
      add: () => c(),
      deleteRow: i,
      copy: f
    })]), u(K, E({
      ...t,
      modelValue: "",
      adddefaultrow: ""
    }, {
      ref: C,
      data: t.modelValue || [],
      columns: O.value
    }), null), d.footer && u("div", {
      class: V.e("footer")
    }, [d.footer({
      addBefore: () => c(!0),
      add: () => c(),
      deleteRow: i,
      copy: f
    })])]);
  }
});
export {
  le as default
};
