{"version":3,"file":"reactive.mjs","names":["isPrimitiveValue","value","Object","reactive","watchedFieldsOrCb","cb","_","ctx","addInitializer","name","thisRoot","isPrimitive","callback","fields","Array","isArray","defineProperty","get","set","newValue","oldValue","call","enumerable","configurable","Proxy","target","p","length","includes"],"sources":["../../src/decorators/reactive.ts"],"sourcesContent":["type Primitive = string | number | boolean | symbol | bigint | null | undefined;\ntype IsReadonly<This, Property extends keyof This> =\n  (<F>() => F extends { [P in Property]: This[Property] } ? 1 : 2) extends\n  (<F>() => F extends { -readonly [P in Property]: This[Property] } ? 1 : 2)\n    ? false\n    : true;\ntype Callback<This> = (this: This, field: string | symbol, newValue: unknown) => void;\ntype ReturnType<\n  This,\n  Property extends keyof This = keyof This,\n> = IsReadonly<This, Property> extends true\n  ? (_: undefined, ctx: ClassFieldDecoratorContext<This, This[Property]>) => void\n  : This[Property] extends Primitive\n    ? (_: undefined, ctx: ClassFieldDecoratorContext<This, This[Property]>) => void\n    : never;\n\nconst isPrimitiveValue = (value: unknown) => value !== Object(value);\n\nfunction reactive<\n  This,\n  Property extends keyof This,\n  Value = This[Property],\n>(cb: Value extends Primitive ? Callback<This> : never): ReturnType<This>;\nfunction reactive<\n  This,\n  Property extends keyof This,\n  Value = This[Property],\n>(watchedFields: Value extends Primitive ? never : Array<keyof Value>, cb: Callback<This>): ReturnType<This>;\n\nfunction reactive<\n  This,\n  Property extends keyof This,\n>(watchedFieldsOrCb: Array<keyof This[Property]> | Callback<This>, cb?: Callback<This>) {\n  return function (_: undefined, ctx: ClassFieldDecoratorContext<This, This[Property]>) {\n    const { addInitializer, name } = ctx;\n\n    addInitializer(function (this: This) {\n      const thisRoot = this;\n\n      const isPrimitive = isPrimitiveValue(this[name as Property]);\n\n      const callback = typeof watchedFieldsOrCb === 'function' ? watchedFieldsOrCb : cb!;\n      const fields = Array.isArray(watchedFieldsOrCb) ? watchedFieldsOrCb : null;\n\n      if (isPrimitive) {\n        let value = this[name as Property];\n\n        Object.defineProperty(this, name, {\n          get() {\n            return value;\n          },\n          set(newValue) {\n            const oldValue = value;\n\n            value = newValue;\n\n            if (oldValue !== newValue) {\n              callback.call(thisRoot, name, newValue);\n            }\n          },\n          enumerable: true,\n          configurable: true,\n        });\n      } else {\n        // @ts-ignore\n        this[name] = new Proxy(this[name], {\n          set(target, p, newValue) {\n            target[p] = newValue;\n\n            if (fields?.length === 0 || fields?.includes(p as keyof This[Property])) {\n              callback.call(thisRoot, p, newValue);\n            }\n\n            return true;\n          },\n        });\n      }\n    });\n  };\n}\n\nexport default reactive;\n"],"mappings":"AAgBA,MAAMA,gBAAgB,GAAIC,KAAc,IAAKA,KAAK,KAAKC,MAAM,CAACD,KAAK,CAAC;AAapE,SAASE,QAAQA,CAGfC,iBAA+D,EAAEC,EAAmB,EAAE;EACtF,OAAO,UAAUC,CAAY,EAAEC,GAAqD,EAAE;IACpF,MAAM;MAAEC,cAAc;MAAEC;IAAK,CAAC,GAAGF,GAAG;IAEpCC,cAAc,CAAC,YAAsB;MACnC,MAAME,QAAQ,GAAG,IAAI;MAErB,MAAMC,WAAW,GAAGX,gBAAgB,CAAC,IAAI,CAACS,IAAI,CAAa,CAAC;MAE5D,MAAMG,QAAQ,GAAG,OAAOR,iBAAiB,KAAK,UAAU,GAAGA,iBAAiB,GAAGC,EAAG;MAClF,MAAMQ,MAAM,GAAGC,KAAK,CAACC,OAAO,CAACX,iBAAiB,CAAC,GAAGA,iBAAiB,GAAG,IAAI;MAE1E,IAAIO,WAAW,EAAE;QACf,IAAIV,KAAK,GAAG,IAAI,CAACQ,IAAI,CAAa;QAElCP,MAAM,CAACc,cAAc,CAAC,IAAI,EAAEP,IAAI,EAAE;UAChCQ,GAAGA,CAAA,EAAG;YACJ,OAAOhB,KAAK;UACd,CAAC;UACDiB,GAAGA,CAACC,QAAQ,EAAE;YACZ,MAAMC,QAAQ,GAAGnB,KAAK;YAEtBA,KAAK,GAAGkB,QAAQ;YAEhB,IAAIC,QAAQ,KAAKD,QAAQ,EAAE;cACzB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