import { defineComponent as c, shallowRef as o, onMounted as w, onBeforeUnmount as E, createElementBlock as O, openBlock as d } from "vue";
function L(M) {
  const D = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let e = "";
  for (let N = 0; N < M; N++)
    e += D.charAt(Math.floor(Math.random() * D.length));
  return e;
}
const x = c({
  name: "IBSheetVue",
  props: {
    options: {
      type: Object,
      required: !0
    },
    data: {
      type: Array,
      default: () => []
    },
    sync: {
      type: Boolean,
      default: !1
    },
    style: {
      type: Object,
      default: () => ({ width: "100%", height: "800px" })
      // 기본 스타일 지정
    },
    exgSheet: {
      type: Object,
      default: null
    }
  },
  emits: ["instance"],
  setup(M, { emit: D }) {
    const e = o(null), N = "ibsheet-container-" + L(10), j = "sheet_" + L(10), y = o(null), t = o(
      null
    );
    function A() {
      if (!M.options)
        throw console.error('[IBSheetVue] required input value "options" not set'), new Error(
          '[IBSheetVue] "options" is a required input; you must provide an IBSheet setting object'
        );
      if (!e.value) {
        console.error("[IBSheetVue] containerRef is not assigned");
        return;
      }
      const u = document.createElement("div");
      u.id = N, u.className = "ibsheet-container", M.style && Object.assign(u.style, M.style), e.value.appendChild(u);
      let i = 0;
      const n = 50, g = 100;
      t.value = setInterval(() => {
        const z = window.IBSheet, l = window.IBSheetLoader;
        if (l && z) {
          if (l._status == 0 && z && z.version) {
            t.value && (clearInterval(t.value), t.value = null);
            try {
              const T = {
                id: j,
                el: u,
                options: M.options,
                data: M.data ?? [],
                sync: M.sync ?? !1
              }, a = z.create(T);
              y.value = a, D("instance", a);
            } catch (T) {
              console.error("Error initializing IBSheet:", T);
            }
          }
          i++, i >= n && (t.value && (clearInterval(t.value), t.value = null), console.error(
            "[initializeIBSheet] IBSheet Initialization Failed: Maximum Retry Exceeded"
          ));
        } else if (z && z.version) {
          t.value && (clearInterval(t.value), t.value = null);
          try {
            const I = {
              id: j,
              el: u,
              options: M.options,
              data: M.data ?? [],
              sync: M.sync ?? !1
            }, T = z.create(I);
            y.value = T, D("instance", T);
          } catch (I) {
            console.error("Error initializing IBSheet:", I);
          }
        } else
          i++, i >= n && (t.value && (clearInterval(t.value), t.value = null), console.error(
            "[initializeIBSheet] IBSheet Initialization Failed: Maximum Retry Exceeded"
          ));
      }, g);
    }
    return w(() => {
      if (M.exgSheet) {
        const u = M.exgSheet, i = document.getElementById(
          u.id
        );
        i && e.value && i.parentElement !== e.value && (i.parentElement?.removeChild(i), e.value.appendChild(i)), y.value = u, D("instance", u);
      } else
        A();
    }), E(() => {
      if (t.value && clearInterval(t.value), y.value?.dispose)
        try {
          y.value.dispose();
        } catch (u) {
          console.warn("Dispose error:", u);
        }
    }), {
      containerRef: e
    };
  }
}), r = (M, D) => {
  const e = M.__vccOpts || M;
  for (const [N, j] of D)
    e[N] = j;
  return e;
}, s = { ref: "containerRef" };
function C(M, D, e, N, j, y) {
  return d(), O("div", s, null, 512);
}
const U = /* @__PURE__ */ r(x, [["render", C]]), h = {
  // 날짜 시간 포멧
  YMD: {
    Type: "Date",
    Align: "Center",
    Width: 110,
    Format: "yyyy/MM/dd",
    DataFormat: "yyyyMMdd",
    EditFormat: "yyyyMMdd",
    Size: 8,
    EmptyValue: "<span style='color:#AAA'>년,월,일 순으로 숫자만 입력해 주세요.</span>"
  },
  YM: {
    Type: "Date",
    Align: "Center",
    Width: 80,
    Format: "yyyy/MM",
    DataFormat: "yyyyMM",
    EditFormat: "yyyyMM",
    Size: 6,
    EmptyValue: "<span style='color:#AAA'>년,월 순으로 숫자만 입력해 주세요.</span>"
  },
  MD: {
    Type: "Date",
    Align: "Center",
    Width: 60,
    Format: "MM/dd",
    EditFormat: "MMdd",
    DataFormat: "MMdd",
    Size: 4,
    EmptyValue: "<span style='color:#AAA'>월,일 순으로 숫자만 입력해 주세요.</span>"
  },
  HMS: {
    Type: "Date",
    Align: "Center",
    Width: 70,
    Format: "HH:mm:ss",
    EditFormat: "HHmmss",
    DataFormat: "HHmmss",
    Size: 8,
    EmptyValue: "<span style='color:#AAA'>시,분,초 순으로 8개 숫자만 입력해 주세요.</span>"
  },
  HM: {
    Type: "Date",
    Align: "Center",
    Width: 70,
    Format: "HH:mm",
    EditFormat: "HHmm",
    DataFormat: "HHmm",
    Size: 6,
    EmptyValue: "<span style='color:#AAA'>시,분 순으로 4개 숫자만 입력해 주세요.</span>"
  },
  YMDHMS: {
    Type: "Date",
    Align: "Center",
    Format: "yyyy/MM/dd HH:mm:ss",
    Width: 150,
    EditFormat: "yyyyMMddHHmmss",
    DataFormat: "yyyyMMddHHmmss",
    Size: 14,
    EmptyValue: "<span style='color:#AAA'>숫자만 입력(ex:20190514153020)</span>"
  },
  YMDHM: {
    Type: "Date",
    Align: "Center",
    Format: "yyyy/MM/dd HH:mm",
    Width: 150,
    EditFormat: "yyyyMMddHHmm",
    DataFormat: "yyyyMMddHHmm",
    Size: 12,
    EmptyValue: "<span style='color:#AAA'>숫자만 입력(ex:201905141530)</span>"
  },
  MDY: {
    Type: "Date",
    Align: "Center",
    Format: "MM-dd-yyyy",
    Width: 110,
    EditFormat: "MMddyyyy",
    DataFormat: "yyyyMMdd",
    Size: 8,
    EmptyValue: "<span style='color:#AAA'>월,일,년 순으로 숫자만 입력해 주세요.</span>"
  },
  DMY: {
    Type: "Date",
    Align: "Center",
    Format: "dd-MM-yyyy",
    Width: 110,
    EditFormat: "ddMMyyyy",
    DataFormat: "yyyyMMdd",
    Size: 8,
    EmptyValue: "<span style='color:#AAA'>일,월,년 순으로 숫자만 입력해 주세요.</span>"
  },
  // 숫자 포멧
  Integer: {
    Type: "Int",
    Align: "Right",
    Format: "#,##0",
    Width: 100
  },
  NullInteger: {
    Type: "Int",
    Align: "Right",
    Format: "#,###",
    Width: 100
  },
  Float: {
    Type: "Float",
    Align: "Right",
    Format: "#,##0.######",
    Width: 100
  },
  NullFloat: {
    Type: "Float",
    Align: "Right",
    Format: "#,###.######",
    Width: 100
  },
  // 기타포멧
  IdNo: {
    Type: "Text",
    Align: "Center",
    CustomFormat: "IdNo"
  },
  SaupNo: {},
  PostNo: {},
  CardNo: {},
  PhoneNo: {},
  Number: {},
  // ibsheet7 migration
  // Popup,PopupEdit
  Popup: {
    Type: "Text",
    Width: 100,
    Align: "Center",
    ButtonFormula: function(M) {
      return M.Sheet.getCanEdit(M.Row, M.Col) ? M.Sheet.Cols[M.Col].Button : "";
    },
    Button: "data:image/svg+xml;base64, "
  },
  // Status Type
  STATUS: {
    Type: "Text",
    Align: "Center",
    Width: 50,
    Formula: "Row.Deleted ? 'D' : Row.Added ? 'I' : Row.Changed ? 'U' : 'R'",
    Format: {
      I: "입력",
      U: "수정",
      D: "삭제",
      R: ""
    }
  },
  // DelCheck Type
  DelCheck: {
    Type: "Bool",
    Width: 50,
    OnClick: function(M) {
      var D = !M.row[M.col], e = M.sheet.getParentRows(M.row);
      return !!(!D && e[0] && e[0][M.col]);
    },
    OnChange: function(M) {
      var D = M.row[M.col];
      if (M.row.Added)
        setTimeout(function() {
          M.sheet.removeRow(M.row);
        }, 30);
      else {
        M.row.Deleted = D;
        var e = M.sheet.getChildRows(M.row);
        e.push(M.row);
        for (var N = !!(e.length > 1 && M.sheet.MainCol), j = 0; j < e.length; j++) {
          var y = e[j];
          M.sheet.setValue(y, M.col, D, N, !0), y.CanEdit = !M.row[M.col], y[M.col + "CanEdit"] || (y[M.col + "CanEdit"] = !0);
        }
      }
    }
  },
  TreeSumFormula: function(M) {
    var D = 0;
    if (M.Row.childNodes.length)
      for (var e = M.Row.firstChild; e; e = e.nextSibling)
        D += e[M.Col];
    else
      return M.Row[M.Col + "CanEdit"] = 1, M.Row[M.Col];
    return D;
  },
  TreeAvgFormula: function(M) {
    var D = 0, e = 0, N = 0;
    if (M.Row.childNodes.length)
      for (var j = M.Row.firstChild; j; j = j.nextSibling)
        e += j[M.Col], N++, D = N && e / N;
    else
      return M.Row[M.Col + "CanEdit"] = 1, M.Row[M.Col];
    return D;
  },
  TreeCountFormula: function(M) {
    var D = 0;
    if (M.Row.childNodes.length)
      for (var e = M.Row.firstChild; e; e = e.nextSibling)
        M.Row[M.Col + "Format"] = "#,##0", D++;
    else
      return M.Row[M.Col + "CanEdit"] = 1, M.Row[M.Col];
    return D;
  },
  TreeMaxFormula: function(M) {
    var D = Number.MIN_VALUE;
    if (M.Row.childNodes.length)
      for (var e = M.Row.firstChild; e; e = e.nextSibling)
        D < e[M.Col] && (D = e[M.Col]);
    else
      return M.Row[M.Col + "CanEdit"] = 1, M.Row[M.Col];
    return D;
  },
  TreeMinFormula: function(M) {
    var D = Number.MAX_VALUE;
    if (M.Row.childNodes.length)
      for (var e = M.Row.firstChild; e; e = e.nextSibling)
        D > e[M.Col] && (D = e[M.Col]);
    else
      return M.Row[M.Col + "CanEdit"] = 1, M.Row[M.Col];
    return D;
  }
};
export {
  U as IBSheetVue,
  h as IB_Preset
};
