import { ref as m, createElementBlock as p, openBlock as f, createElementVNode as l, toDisplayString as h, withDirectives as D, createCommentVNode as V, normalizeClass as x, vModelText as y } from "vue";
/* empty css                 */
import z from "../../../_virtual/_plugin-vue_export-helper.js";
const N = { class: "create-chat-form" }, S = { class: "create-chat-title" }, T = { class: "form-field" }, U = {
  key: 0,
  class: "form-error"
}, q = { class: "form-field" }, w = {
  key: 0,
  class: "form-error"
}, M = {
  __name: "CreateChat2",
  props: {
    title: {
      type: String,
      required: !0
    }
  },
  emits: ["change"],
  setup(C, { expose: E, emit: P }) {
    const r = m({}), a = m({
      name: "",
      phone: ""
    }), c = m(!1), b = P, k = () => {
      a.value = {
        name: "",
        phone: ""
      };
    }, B = () => {
      c.value = !1;
    }, F = (t, e) => {
      a.value[t] = e;
    }, d = (t) => !t || t.trim().length < 3 ? "Имя должно содержать минимум 3 символа" : "", v = (t) => {
      if (!c.value)
        return "";
      if (!t)
        return "Телефон обязателен для заполнения";
      const e = t.replace(/\D/g, "");
      return e.length < 11 ? "Минимум 11 цифр" : e.startsWith("7") ? "" : "Номер телефона должен начинаться с цифры 7";
    }, I = (t) => {
      if (!t) return !1;
      const e = t.replace(/\D/g, "");
      return !(e.length < 11 || !e.startsWith("7"));
    }, s = () => {
      const t = d(r.value.name), e = v(r.value.phone), n = I(r.value.phone);
      return a.value = {
        name: t,
        phone: e
      }, !t && n;
    }, u = (t) => (t || "").replace(/\D/g, ""), g = (t) => {
      const e = u(t);
      let n = e;
      if (e.startsWith("8") && (n = "7" + e.slice(1)), n.startsWith("7") || (n = "7" + n), n = n.slice(0, 11), n.length <= 1)
        return n ? "+" + n : "";
      const i = "+7";
      if (n.length > 1) {
        const o = n.slice(1);
        return o.length <= 3 ? i + " (" + o : o.length <= 6 ? i + " (" + o.slice(0, 3) + ") " + o.slice(3) : o.length <= 8 ? i + " (" + o.slice(0, 3) + ") " + o.slice(3, 6) + "-" + o.slice(6) : i + " (" + o.slice(0, 3) + ") " + o.slice(3, 6) + "-" + o.slice(6, 8) + "-" + o.slice(8);
      }
      return i;
    }, W = () => {
      const t = r.value.phone ?? "", e = g(t);
      e !== t && (r.value = {
        ...r.value,
        phone: e
      });
    }, _ = () => {
      W(), s(), b("change", {
        contact: r.value,
        contactDigits: u(r.value.phone),
        isValid: s()
      });
    };
    return E({
      clearValidationErrors: k,
      resetTouchedState: B,
      setValidationError: F,
      validateForm: s,
      validateName: d,
      validatePhone: v,
      formatPhone: g,
      extractDigits: u,
      validationErrors: a,
      phoneInputTouched: c
    }), (t, e) => (f(), p("div", N, [
      l("h2", S, h(C.title), 1),
      l("div", T, [
        e[3] || (e[3] = l("label", {
          class: "form-label",
          for: "contact-name"
        }, "Имя", -1)),
        D(l("input", {
          id: "contact-name",
          "onUpdate:modelValue": e[0] || (e[0] = (n) => r.value.name = n),
          name: "name",
          type: "text",
          class: x(["form-input", { "form-input--error": a.value.name }]),
          placeholder: "Введите имя",
          onInput: _,
          onBlur: s
        }, null, 34), [
          [y, r.value.name]
        ]),
        a.value.name ? (f(), p("div", U, h(a.value.name), 1)) : V("", !0)
      ]),
      l("div", q, [
        e[4] || (e[4] = l("label", {
          class: "form-label",
          for: "contact-phone"
        }, "Телефон", -1)),
        D(l("input", {
          id: "contact-phone",
          "onUpdate:modelValue": e[1] || (e[1] = (n) => r.value.phone = n),
          name: "phone",
          type: "text",
          class: x(["form-input", { "form-input--error": a.value.phone }]),
          placeholder: "+7 (999) 999-99-99",
          onInput: _,
          onFocus: e[2] || (e[2] = (n) => c.value = !0),
          onBlur: s
        }, null, 34), [
          [y, r.value.phone]
        ]),
        a.value.phone ? (f(), p("div", w, h(a.value.phone), 1)) : V("", !0)
      ])
    ]));
  }
}, G = /* @__PURE__ */ z(M, [["__scopeId", "data-v-3302f415"]]);
export {
  G as default
};
