{"version":3,"file":"proxiedModel.mjs","names":["useToggleScope","computed","ref","toRaw","watch","getCurrentInstance","toKebabCase","useProxiedModel","props","prop","defaultValue","transformIn","arguments","length","undefined","v","transformOut","vm","internal","kebabProp","checkKebab","isControlled","vnode","hasOwnProperty","value","val","model","get","externalValue","set","internalValue","newValue","emit","Object","defineProperty"],"sources":["../../src/composables/proxiedModel.ts"],"sourcesContent":["// Composables\nimport { useToggleScope } from '@/composables/toggleScope'\n\n// Utilities\nimport { computed, ref, toRaw, watch } from 'vue'\nimport { getCurrentInstance, toKebabCase } from '@/util'\n\n// Types\nimport type { Ref } from 'vue'\n\ntype InnerVal = T extends any[] ? Readonly : T\n\n// Composables\nexport function useProxiedModel<\n Props extends object & { [key in Prop as `onUpdate:${Prop}`]: ((val: any) => void) | undefined },\n Prop extends Extract,\n Inner = Props[Prop],\n> (\n props: Props,\n prop: Prop,\n defaultValue?: Props[Prop],\n transformIn: (value?: Props[Prop]) => Inner = (v: any) => v,\n transformOut: (value: Inner) => Props[Prop] = (v: any) => v,\n) {\n const vm = getCurrentInstance('useProxiedModel')\n const internal = ref(props[prop] !== undefined ? props[prop] : defaultValue) as Ref\n const kebabProp = toKebabCase(prop)\n const checkKebab = kebabProp !== prop\n\n const isControlled = checkKebab\n ? computed(() => {\n void props[prop]\n return !!(\n (vm.vnode.props?.hasOwnProperty(prop) || vm.vnode.props?.hasOwnProperty(kebabProp)) &&\n (vm.vnode.props?.hasOwnProperty(`onUpdate:${prop}`) || vm.vnode.props?.hasOwnProperty(`onUpdate:${kebabProp}`))\n )\n })\n : computed(() => {\n void props[prop]\n return !!(vm.vnode.props?.hasOwnProperty(prop) && vm.vnode.props?.hasOwnProperty(`onUpdate:${prop}`))\n })\n\n useToggleScope(() => !isControlled.value, () => {\n watch(() => props[prop], val => {\n internal.value = val\n })\n })\n\n const model = computed({\n get (): any {\n const externalValue = props[prop]\n return transformIn(isControlled.value ? externalValue : internal.value)\n },\n set (internalValue) {\n const newValue = transformOut(internalValue)\n const value = toRaw(isControlled.value ? props[prop] : internal.value)\n if (value === newValue || transformIn(value) === internalValue) {\n return\n }\n internal.value = newValue\n vm?.emit(`update:${prop}`, newValue)\n },\n }) as any as Ref> & { readonly externalValue: Props[Prop] }\n\n Object.defineProperty(model, 'externalValue', {\n get: () => isControlled.value ? props[prop] : internal.value,\n })\n\n return model\n}\n"],"mappings":"AAAA;AAAA,SACSA,cAAc,6BAEvB;AACA,SAASC,QAAQ,EAAEC,GAAG,EAAEC,KAAK,EAAEC,KAAK,QAAQ,KAAK;AAAA,SACxCC,kBAAkB,EAAEC,WAAW,6BAExC;AAKA;AACA,OAAO,SAASC,eAAeA,CAK7BC,KAAY,EACZC,IAAU,EACVC,YAA0B,EAG1B;EAAA,IAFAC,WAA2C,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAIG,CAAM,IAAKA,CAAC;EAAA,IAC3DC,YAA2C,GAAAJ,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAIG,CAAM,IAAKA,CAAC;EAE3D,MAAME,EAAE,GAAGZ,kBAAkB,CAAC,iBAAiB,CAAC;EAChD,MAAMa,QAAQ,GAAGhB,GAAG,CAACM,KAAK,CAACC,IAAI,CAAC,KAAKK,SAAS,GAAGN,KAAK,CAACC,IAAI,CAAC,GAAGC,YAAY,CAAqB;EAChG,MAAMS,SAAS,GAAGb,WAAW,CAACG,IAAI,CAAC;EACnC,MAAMW,UAAU,GAAGD,SAAS,KAAKV,IAAI;EAErC,MAAMY,YAAY,GAAGD,UAAU,GAC3BnB,QAAQ,CAAC,MAAM;IACf,KAAKO,KAAK,CAACC,IAAI,CAAC;IAChB,OAAO,CAAC,EACN,CAACQ,EAAE,CAACK,KAAK,CAACd,KAAK,EAAEe,cAAc,CAACd,IAAI,CAAC,IAAIQ,EAAE,CAACK,KAAK,CAACd,KAAK,EAAEe,cAAc,CAACJ,SAAS,CAAC,MACj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