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