import { defineComponent as L, mergeModels as M, useModel as k, ref as w, computed as E, onMounted as V, createElementBlock as s, openBlock as u, normalizeStyle as b, normalizeClass as d, unref as o, createElementVNode as c, createCommentVNode as h, withDirectives as C, toDisplayString as g, withKeys as v, vModelText as T } from "vue";
import { useTransformedModel as B } from "../../composition/useTransformedModel.js";
import m from "./pl-editable-title.module.scss.js";
const S = { key: 0 }, W = ["placeholder"], N = /* @__PURE__ */ L({
  __name: "PlEditableTitle",
  props: /* @__PURE__ */ M({
    placeholder: { default: "Title" },
    maxWidth: { default: "80%" },
    prefix: { default: void 0 },
    maxLength: { default: 1e3 },
    minLength: { default: void 0 },
    autofocus: { type: Boolean }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(f) {
    const a = k(f, "modelValue"), r = f, t = B(a, {
      update() {
        return !1;
      },
      parse: (e) => {
        if (typeof e != "string")
          throw Error("value should be a string");
        if (r.maxLength && e.length > r.maxLength)
          throw Error(`Max title length is ${r.maxLength} characters`);
        if (r.minLength && e.length < r.minLength)
          throw Error(`Min title length is ${r.minLength} characters`);
        return e.trim();
      }
    }), i = w(), y = E(() => ({
      maxWidth: r.maxWidth ?? "80%"
    })), x = () => {
      a.value = t.value && !t.error ? t.value : a.value, t.reset();
    };
    return V(() => {
      var e;
      r.autofocus && ((e = i.value) == null || e.focus());
    }), (e, l) => (u(), s("div", {
      class: d(["pl-editable-title", o(m).component]),
      style: b(y.value)
    }, [
      c("div", {
        class: d(o(m).container),
        onClick: l[3] || (l[3] = () => {
          var n;
          return (n = i.value) == null ? void 0 : n.focus();
        })
      }, [
        e.prefix ? (u(), s("span", S, g(e.prefix.trim()) + " ", 1)) : h("", !0),
        C(c("input", {
          ref_key: "inputRef",
          ref: i,
          "onUpdate:modelValue": l[0] || (l[0] = (n) => o(t).value = n),
          placeholder: e.placeholder,
          onFocusout: x,
          onKeydown: [
            l[1] || (l[1] = v(
              //@ts-ignore
              (...n) => o(t).reset && o(t).reset(...n),
              ["escape"]
            )),
            l[2] || (l[2] = v((n) => {
              var p;
              return (p = n.target) == null ? void 0 : p.blur();
            }, ["enter"]))
          ]
        }, null, 40, W), [
          [T, o(t).value]
        ])
      ], 2),
      o(t).error ? (u(), s("div", {
        key: 0,
        class: d(o(m).error)
      }, g(o(t).error), 3)) : h("", !0)
    ], 6));
  }
});
export {
  N as default
};
//# sourceMappingURL=PlEditableTitle.vue.js.map
