import { w as I, x as _, G as W, k as i, J as w, l as e, K as R, V as P, Z as Q, Q as L, o as B, aj as te, a8 as X, F as ae, u as ie, n as Z, P as Y, W as re, b6 as H, ay as le, aJ as se, aK as de, aL as he, aM as ce, aN as ue, aO as pe, aP as ee, am as j, B as k, T as f, aR as A, aS as z, I as D, m as $, v as be, q as ge, C as me } from "./App-1cb933e1.mjs";
import h from "react";
function U({
  formRef: p,
  type: t
}) {
  var o, d, O, F, V, y, S;
  const b = p.watch(), l = I.cloneDeep(((o = b.webhook) == null ? void 0 : o[t]) || []), M = h.useCallback(() => p.setValue(`webhook.${t}`, [...l, {
    id: _(3),
    value: ["", ""]
  }]), [l]), {
    setIsModify: C
  } = W.useActions(), v = W.useSelectState((c) => c.isModify), g = h.useCallback((c, u) => {
    const m = l.findIndex((E) => E.id === u);
    if (m < 0)
      return;
    l[m].value[0] && (l[m].value[1] = c.target.value, p.setValue(`webhook.${t}`, l)), !v && C(!0);
  }, [(d = b.webhook) == null ? void 0 : d.headers, (O = b.webhook) == null ? void 0 : O.body, l, v]), n = h.useCallback((c, u) => {
    const m = l.findIndex((T) => T.id === u);
    m < 0 || (l[m].value[0] = c.target.value, p.setValue(`webhook.${t}`, l), !v && C(!0));
  }, [(F = b.webhook) == null ? void 0 : F.headers, (V = b.webhook) == null ? void 0 : V.body, l, v]), s = h.useCallback((c) => {
    const u = l.findIndex((m) => m.id === c);
    l.splice(u, 1), !(u < 0) && (p.setValue(`webhook.${t}`, l), !v && C(!0));
  }, [(y = b.webhook) == null ? void 0 : y.headers, (S = b.webhook) == null ? void 0 : S.body, l, v]);
  return /* @__PURE__ */ i(w, {
    children: [/* @__PURE__ */ e(R, {
      children: I.capitalize(t)
    }), /* @__PURE__ */ i(P, {
      alignItems: "start",
      children: [l == null ? void 0 : l.map((c) => /* @__PURE__ */ i(Q, {
        children: [/* @__PURE__ */ e(L, {
          width: "45%",
          placeholder: "Property",
          value: c.value[0],
          onChange: (u) => n(u, c.id)
        }), /* @__PURE__ */ e(L, {
          width: "45%",
          placeholder: "Value",
          value: c.value[1],
          onChange: (u) => g(u, c.id)
        }), /* @__PURE__ */ e(B, {
          size: "sm",
          variant: "outline",
          onClick: (u) => s(c.id),
          children: /* @__PURE__ */ e(te, {
            fontSize: "24px"
          })
        })]
      }, c.id)), /* @__PURE__ */ i(B, {
        leftIcon: /* @__PURE__ */ e(X, {
          fontSize: "20px",
          color: "#0065FF"
        }),
        size: "xs",
        variant: "link",
        color: "#0065FF",
        onClick: M,
        _focus: {
          borderWidth: "0px"
        },
        _hover: {
          textDecoration: "none"
        },
        fontWeight: "normal",
        children: ["Add ", t === "headers" ? t.slice(0, -1) : t]
      })]
    })]
  });
}
const J = "4px 0px 0px 4px", G = "0px 4px 4px 0px", ne = [{
  label: "Insert",
  value: "INSERT"
}, {
  label: "Update",
  value: "UPDATE"
}, {
  label: "Delete",
  value: "DELETE"
}];
function ve({
  modalDisclosure: p,
  table: t
}) {
  var u, m, T, E;
  const b = h.useRef(null), [l, M] = h.useState(!1), C = ae(), v = ie(), g = Z(), n = h.useMemo(() => ({
    table: (t == null ? void 0 : t.name) || "",
    name: "",
    event: void 0,
    destination: void 0,
    webhook: {
      contentType: "application/json",
      method: void 0,
      url: "",
      headers: [],
      body: []
    }
  }), [t == null ? void 0 : t.name]), s = Y({
    defaultValues: n,
    mode: "onChange"
  }), o = s.watch(), d = re(), O = h.useCallback(async (r) => {
    try {
      M(!0), await v.migrateTrigger({
        operation: "Create",
        migration: {
          ...r,
          webhook: {
            ...r.webhook,
            headers: H(r.webhook.headers),
            body: H(r.webhook.body)
          }
        }
      }), await g.revalidate(), C.push(`/tables/${(t == null ? void 0 : t.name) || ""}/triggers/${I.snakeCase(r.name)}`), y();
    } catch (a) {
      const x = a.response.data.message.split("-");
      d({
        description: `${x[x.length - 2]}`,
        status: "error",
        isClosable: !0
      });
    }
    M(!1);
  }, [g, t]), F = h.useCallback((r) => {
    r.value === "UPDATE" ? o.destination = "" : delete o.destination, s.reset(o), s.setValue("event", r.value);
  }, [s, o]), V = h.useCallback((r) => {
    s.setValue("destination", r.value);
  }, [s]), y = h.useCallback(() => {
    var r;
    (r = p == null ? void 0 : p.onClose) == null || r.call(p), s.reset(n);
  }, [s, p, n]), S = h.useMemo(() => !!Object.entries(o).filter(([a, x]) => I.isEmpty(x) || // @ts-ignore
  a === "webhook" && (!(x != null && x.url) || !(x != null && x.method))).length, [o]), c = h.useCallback((r) => {
    s.setValue("webhook.method", r.value), r.value === "GET" && s.setValue("webhook.body", []);
  }, []);
  return /* @__PURE__ */ e(le, {
    children: /* @__PURE__ */ i(se, {
      isOpen: p.isOpen || !1,
      onClose: y,
      isCentered: !0,
      initialFocusRef: b,
      closeOnEsc: !1,
      size: "xl",
      children: [/* @__PURE__ */ e(de, {}), /* @__PURE__ */ i(he, {
        children: [/* @__PURE__ */ e(ce, {
          children: "Create Event Trigger"
        }), /* @__PURE__ */ e(ue, {}), /* @__PURE__ */ e(pe, {
          children: /* @__PURE__ */ e("form", {
            onSubmit: s.handleSubmit(O),
            children: /* @__PURE__ */ i(P, {
              spacing: 5,
              width: "100%",
              marginBottom: "16px",
              children: [/* @__PURE__ */ i(w, {
                isRequired: !0,
                children: [/* @__PURE__ */ e(R, {
                  htmlFor: "title",
                  children: "Trigger name"
                }), /* @__PURE__ */ e(L, {
                  id: "title",
                  ref: b,
                  fontSize: "14px",
                  isDisabled: l,
                  placeholder: "Type trigger name",
                  onChange: (r) => {
                    s.setValue("name", I.snakeCase(r.currentTarget.value));
                  }
                }), ((u = s.formState.errors.name) == null ? void 0 : u.message) && /* @__PURE__ */ e(ee, {
                  children: (m = s.formState.errors.name) == null ? void 0 : m.message
                })]
              }), /* @__PURE__ */ i(w, {
                isRequired: !0,
                children: [/* @__PURE__ */ e(R, {
                  children: "Trigger Operations"
                }), /* @__PURE__ */ i(j, {
                  alignItems: "center",
                  children: [/* @__PURE__ */ e(k, {
                    padding: "0.4rem",
                    paddingInline: "15px",
                    bgColor: "gray.100",
                    borderRadius: J,
                    borderLeft: "0px",
                    border: "1px solid",
                    borderColor: "rgb(226, 232, 240)",
                    height: "35px",
                    maxWidth: "70%",
                    children: /* @__PURE__ */ e(f, {
                      children: "When"
                    })
                  }), /* @__PURE__ */ e(k, {
                    flex: 1,
                    children: /* @__PURE__ */ e(A, {
                      style: {
                        borderRadius: G,
                        height: 35
                      },
                      options: ne,
                      onChange: F
                    })
                  })]
                })]
              }), /* @__PURE__ */ e(z, {
                in: o.event === "UPDATE",
                style: {
                  width: "100%",
                  overflow: "inherit"
                },
                children: /* @__PURE__ */ e(w, {
                  children: /* @__PURE__ */ i(j, {
                    alignItems: "center",
                    children: [/* @__PURE__ */ e(k, {
                      padding: "0.4rem",
                      paddingInline: "15px",
                      bgColor: "gray.100",
                      borderRadius: J,
                      borderLeft: "0px",
                      border: "1px solid",
                      borderColor: "rgb(226, 232, 240)",
                      height: "35px",
                      maxWidth: "70%",
                      children: /* @__PURE__ */ e(f, {
                        children: "On column"
                      })
                    }), /* @__PURE__ */ e(k, {
                      flex: 1,
                      children: /* @__PURE__ */ e(A, {
                        options: t == null ? void 0 : t.columns.map((r) => ({
                          label: r.name,
                          value: r.name
                        })),
                        style: {
                          borderRadius: G,
                          height: 35
                        },
                        value: o.destination ? {
                          label: o.destination,
                          value: o.destination
                        } : null,
                        onChange: V
                      })
                    })]
                  })
                })
              }), /* @__PURE__ */ i(w, {
                children: [/* @__PURE__ */ e(R, {
                  children: "Webhook"
                }), /* @__PURE__ */ i(P, {
                  padding: "20px",
                  paddingX: "15px",
                  backgroundColor: "#F9F9FA",
                  borderRadius: "4px",
                  spacing: 4,
                  children: [/* @__PURE__ */ i(D, {
                    children: [/* @__PURE__ */ i($, {
                      height: "30px",
                      children: [/* @__PURE__ */ e(f, {
                        children: "URL"
                      }), /* @__PURE__ */ e(f, {
                        color: "red",
                        marginInlineStart: 1,
                        children: "*"
                      })]
                    }), /* @__PURE__ */ e(L, {
                      height: "30px",
                      type: "url",
                      value: (T = o.webhook) == null ? void 0 : T.url,
                      placeholder: "https://webhook.site/",
                      ...s.register("webhook.url")
                    })]
                  }), /* @__PURE__ */ e(U, {
                    formRef: s,
                    type: "headers"
                  }), /* @__PURE__ */ i(D, {
                    children: [/* @__PURE__ */ i($, {
                      height: "30px",
                      children: [/* @__PURE__ */ e(f, {
                        children: "Method"
                      }), /* @__PURE__ */ e(f, {
                        color: "red",
                        marginInlineStart: 1,
                        children: "*"
                      })]
                    }), /* @__PURE__ */ e(k, {
                      flex: 1,
                      children: /* @__PURE__ */ e(A, {
                        style: {
                          borderRadius: G,
                          height: 30
                        },
                        options: q.map((r) => ({
                          label: r,
                          value: r
                        })),
                        onChange: c
                      })
                    })]
                  }), /* @__PURE__ */ e(z, {
                    in: ((E = o.webhook) == null ? void 0 : E.method) !== "GET",
                    style: {
                      width: "100%",
                      overflow: "inherit"
                    },
                    children: /* @__PURE__ */ e(U, {
                      formRef: s,
                      type: "body"
                    })
                  })]
                })]
              }), /* @__PURE__ */ i(B, {
                width: "100%",
                colorScheme: "blue",
                type: "submit",
                variant: "solid",
                fontSize: "16px",
                isLoading: l,
                isDisabled: S,
                children: [/* @__PURE__ */ e(X, {
                  fontSize: "22px",
                  style: {
                    marginRight: "3px"
                  }
                }), /* @__PURE__ */ e(f, {
                  paddingTop: "2px",
                  children: "Create"
                })]
              })]
            })
          })
        })]
      })]
    })
  });
}
const K = "4px 0px 0px 4px", N = "0px 4px 4px 0px", q = ["GET", "POST", "PATCH", "PUT", "DELETE"];
function xe() {
  var y, S, c, u, m, T, E, r;
  const p = Z(), t = be(), {
    setIsModify: b,
    setEventTriggerMigration: l,
    setEventTriggerError: M
  } = W.useActions(), C = W.useSelectState((a) => a.isModify), v = W.useSelectState((a) => a.eventTriggerMigration.isErrorSubmit), g = h.useMemo(() => {
    var a, x;
    return (x = (a = p.data) == null ? void 0 : a.data) == null ? void 0 : x.tables.find((oe) => oe.name === t.tableName);
  }, [(S = (y = p.data) == null ? void 0 : y.data) == null ? void 0 : S.tables, t.tableName]), n = h.useMemo(() => g == null ? void 0 : g.triggers.find((a) => a.name === t.triggerName), [g == null ? void 0 : g.triggers, t.triggerName]), s = h.useMemo(() => {
    if (n)
      return {
        table: t.tableName,
        name: (n == null ? void 0 : n.name) || "",
        event: n == null ? void 0 : n.event,
        ...(n == null ? void 0 : n.destination) && {
          destination: n == null ? void 0 : n.destination
        },
        webhook: {
          contentType: "application/json",
          method: n == null ? void 0 : n.webhook.method,
          url: n == null ? void 0 : n.webhook.url,
          headers: Object.entries((n == null ? void 0 : n.webhook.headers) || {}).map((a) => ({
            value: a,
            id: _(3)
          })),
          body: Object.entries((n == null ? void 0 : n.webhook.body) || {}).map((a) => ({
            value: a,
            id: _(3)
          }))
        }
      };
  }, [n == null ? void 0 : n.name, n == null ? void 0 : n.event, n == null ? void 0 : n.destination, n == null ? void 0 : n.webhook, t.tableName]), o = Y({
    mode: "onChange"
  });
  h.useEffect(() => {
    (s || v) && (o.reset(I.cloneDeep(s)), v && M(!1));
  }, [s, v]);
  const d = o.watch();
  h.useEffect(() => {
    l(I.cloneDeep(d));
  }, [Object.values(d)]);
  const O = h.useCallback((a) => {
    a.value === "UPDATE" ? d.destination = "" : delete d.destination, o.reset(d), o.setValue("event", a.value), !C && b(!0);
  }, [o, d, C]), F = h.useCallback((a) => {
    o.setValue("destination", a.value), !C && b(!0);
  }, [o, C]), V = h.useCallback((a) => {
    o.setValue("webhook.method", a.value), a.value === "GET" && o.setValue("webhook.body", []), !C && b(!0);
  }, [C]);
  return /* @__PURE__ */ i(P, {
    flex: 1,
    alignItems: "normal",
    height: "full",
    overflow: "auto",
    spacing: 0,
    children: [/* @__PURE__ */ e(ge, {}), /* @__PURE__ */ e(me, {
      backgroundColor: "#E5E5E5",
      height: "full",
      children: /* @__PURE__ */ e("form", {
        style: {
          backgroundColor: "white",
          padding: "50px",
          borderRadius: "4px"
        },
        children: /* @__PURE__ */ i(P, {
          spacing: 5,
          width: "60vh",
          marginBottom: "16px",
          children: [/* @__PURE__ */ i(w, {
            isRequired: !0,
            children: [/* @__PURE__ */ e(R, {
              htmlFor: "title",
              children: "Trigger name"
            }), /* @__PURE__ */ e(L, {
              id: "title",
              fontSize: "14px",
              placeholder: "Type trigger name",
              value: d.name,
              ...o.register("name", {
                onChange: () => b(!0)
              })
            }), ((c = o.formState.errors.name) == null ? void 0 : c.message) && /* @__PURE__ */ e(ee, {
              children: (u = o.formState.errors.name) == null ? void 0 : u.message
            })]
          }), /* @__PURE__ */ i(w, {
            isRequired: !0,
            children: [/* @__PURE__ */ e(R, {
              children: "Trigger Operations"
            }), /* @__PURE__ */ i(j, {
              alignItems: "center",
              children: [/* @__PURE__ */ e(k, {
                padding: "0.4rem",
                paddingInline: "15px",
                bgColor: "gray.100",
                borderRadius: K,
                borderLeft: "0px",
                border: "1px solid",
                borderColor: "rgb(226, 232, 240)",
                height: "35px",
                maxWidth: "70%",
                children: /* @__PURE__ */ e(f, {
                  children: "When"
                })
              }), /* @__PURE__ */ e(k, {
                flex: 1,
                children: /* @__PURE__ */ e(A, {
                  style: {
                    borderRadius: N,
                    height: 35
                  },
                  options: ne,
                  onChange: O,
                  value: d.event ? {
                    label: d.event,
                    value: d.event
                  } : null
                })
              })]
            })]
          }), /* @__PURE__ */ e(z, {
            in: d.event === "UPDATE",
            style: {
              width: "100%",
              overflow: "inherit"
            },
            children: /* @__PURE__ */ e(w, {
              children: /* @__PURE__ */ i(j, {
                alignItems: "center",
                children: [/* @__PURE__ */ e(k, {
                  padding: "0.4rem",
                  paddingInline: "15px",
                  bgColor: "gray.100",
                  borderRadius: K,
                  borderLeft: "0px",
                  border: "1px solid",
                  borderColor: "rgb(226, 232, 240)",
                  height: "35px",
                  maxWidth: "70%",
                  children: /* @__PURE__ */ e(f, {
                    children: "On column"
                  })
                }), /* @__PURE__ */ e(k, {
                  flex: 1,
                  children: /* @__PURE__ */ e(A, {
                    options: g == null ? void 0 : g.columns.map((a) => ({
                      label: a.name,
                      value: a.name
                    })),
                    style: {
                      borderRadius: N,
                      height: 35
                    },
                    value: d.destination ? {
                      label: d.destination,
                      value: d.destination
                    } : null,
                    onChange: F
                  })
                })]
              })
            })
          }), /* @__PURE__ */ i(w, {
            children: [/* @__PURE__ */ e(R, {
              children: "Webhook"
            }), /* @__PURE__ */ i(P, {
              padding: "20px",
              paddingX: "15px",
              backgroundColor: "#F9F9FA",
              borderRadius: "4px",
              spacing: 4,
              width: "full",
              children: [/* @__PURE__ */ i(D, {
                children: [/* @__PURE__ */ e($, {
                  height: "30px",
                  children: /* @__PURE__ */ i(Q, {
                    spacing: 0.75,
                    children: [/* @__PURE__ */ e(f, {
                      children: "URL"
                    }), /* @__PURE__ */ e(f, {
                      color: "red",
                      marginInlineStart: 1,
                      children: "*"
                    })]
                  })
                }), /* @__PURE__ */ e(L, {
                  height: "30px",
                  type: "url",
                  value: (m = d.webhook) == null ? void 0 : m.url,
                  placeholder: "https://webhook.site/",
                  ...o.register("webhook.url", {
                    onChange: () => b(!0)
                  })
                })]
              }), /* @__PURE__ */ e(U, {
                formRef: o,
                type: "headers"
              }), /* @__PURE__ */ i(D, {
                children: [/* @__PURE__ */ i($, {
                  height: "30px",
                  children: [/* @__PURE__ */ e(f, {
                    children: "Method"
                  }), /* @__PURE__ */ e(f, {
                    color: "red",
                    marginInlineStart: 1,
                    children: "*"
                  })]
                }), /* @__PURE__ */ e(k, {
                  flex: 1,
                  children: /* @__PURE__ */ e(A, {
                    style: {
                      borderRadius: N,
                      height: 30
                    },
                    options: q.map((a) => ({
                      label: a,
                      value: a
                    })),
                    value: {
                      label: (T = d.webhook) == null ? void 0 : T.method,
                      value: (E = d.webhook) == null ? void 0 : E.method
                    },
                    onChange: V
                  })
                })]
              }), /* @__PURE__ */ e(z, {
                in: ((r = d.webhook) == null ? void 0 : r.method) !== "GET",
                style: {
                  width: "100%",
                  overflow: "inherit"
                },
                children: /* @__PURE__ */ e(U, {
                  formRef: o,
                  type: "body"
                })
              })]
            })]
          })]
        })
      })
    })]
  });
}
const ke = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: xe,
  methodOptions: q
}, Symbol.toStringTag, { value: "Module" }));
export {
  ve as C,
  ke as E
};
