{"version":3,"file":"defineElement-DkVrHaue.cjs","sources":["../../../src/utils/dummyFn.ts","../../../src/utils/is.ts","../../../src/utils/browser.ts","../../../src/defineElement/hooks.ts","../../../src/defineElement/defineElement.ts"],"sourcesContent":["import type { Fn } from './types'\n\nexport const dummyFn: Fn = <Result>(): Result => {\n  return void 0 as unknown as Result\n}\n","import type { Fn } from './types'\n\nconst is = (value: unknown, type: string): boolean => Object.prototype.toString.call(value) === `[object ${type}]`\n\nexport const isArray = Array.isArray\nexport const isFunction = (value: unknown): value is Fn => is(value, 'Function')\nexport const isObject = (value: unknown): value is typeof Object => is(value, 'Object')\nexport const isString = (value: unknown): value is string => is(value, 'String')\nexport const isUndefined = (value: unknown): value is undefined => value === undefined\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport const isSubclassOf = (subclass: Function, superclass: Function) =>\n  subclass === superclass || Object.prototype.isPrototypeOf.call(superclass.prototype, subclass.prototype)\nexport const isClass = (value: unknown): value is new (...args: any[]) => unknown =>\n  isFunction(value) && Function.prototype.toString.call(value).trim().startsWith('class')\nexport const isNumber = (value: unknown): value is number => value != null && value === +value\n","import type { ValidCustomElementName } from './ValidCustomElementName'\n\nexport const registerCustomElement = (name: ValidCustomElementName, constructor: CustomElementConstructor) =>\n  customElements.define(name, constructor)\n","import type { LitElement } from 'lit'\nimport { type PropertyValues, type ReactiveElement } from 'lit'\nimport { DEFINE_COMPONENT_OPTIONS_SYMBOL } from '../symbols'\nimport { getCurrentOptions } from '../currentInstance'\nimport { dummyFn } from '../utils/dummyFn'\n\ntype LitRender = LitElement['render']\n\n/**\n * @internal\n */\ninterface InternalOptions<DefinedComponents extends ReactiveElement> {\n  render: LitRender\n  connectedCallback: FnSubscriber<() => void>\n  disconnectedCallback: FnSubscriber<() => void>\n  performUpdate: FnSubscriber<() => void>\n  shouldUpdate: FnSubscriber<ChangedPropertiesPredicate<DefinedComponents>>\n  willUpdate: FnSubscriber<ChangedPropertiesConsumer<DefinedComponents>>\n  update: FnSubscriber<ChangedPropertiesConsumer<DefinedComponents>>\n  firstUpdated: FnSubscriber<ChangedPropertiesConsumer<DefinedComponents>>\n  updated: FnSubscriber<ChangedPropertiesConsumer<DefinedComponents>>\n}\n\ntype ChangedPropertiesPredicate<DefinedComponents extends ReactiveElement> = (\n  changedProperties: PropertyValues<DefinedComponents>\n) => boolean\n\ntype ChangedPropertiesConsumer<DefinedComponents extends ReactiveElement> = (\n  changedProperties: PropertyValues<DefinedComponents>\n) => void\n\nconst fnSubscriber = <FN extends (...args: any[]) => unknown>() => {\n  const subscribers = [] as FN[]\n  return {\n    add: (fn: FN) => subscribers.push(fn),\n    run: (t: unknown, ...args: Parameters<FN>) => subscribers.map((fn) => fn.apply(t, args)),\n  }\n}\n\ntype FnSubscriber<T extends (...args: any[]) => unknown> = ReturnType<typeof fnSubscriber<T>>\n\nexport const withHooks = <\n  Base extends abstract new (...args: any[]) => LitElement,\n  Result = abstract new () => LitElement &\n    InstanceType<Base> & {\n      __opts: InternalOptions<InstanceType<Base>>\n    },\n>(\n  Base: Base\n): Result => {\n  abstract class Mixin extends Base {\n    /** @internal */\n    [DEFINE_COMPONENT_OPTIONS_SYMBOL]: InternalOptions<this> = {\n      render: dummyFn,\n      connectedCallback: fnSubscriber(),\n      disconnectedCallback: fnSubscriber(),\n      willUpdate: fnSubscriber<ChangedPropertiesConsumer<typeof this>>(),\n      performUpdate: fnSubscriber(),\n      shouldUpdate: fnSubscriber<ChangedPropertiesPredicate<typeof this>>(),\n      update: fnSubscriber<ChangedPropertiesConsumer<typeof this>>(),\n      firstUpdated: fnSubscriber<ChangedPropertiesConsumer<typeof this>>(),\n      updated: fnSubscriber<ChangedPropertiesConsumer<typeof this>>(),\n    }\n\n    protected get __opts() {\n      return this[DEFINE_COMPONENT_OPTIONS_SYMBOL]\n    }\n\n    connectedCallback() {\n      super.connectedCallback()\n      return this.__opts.connectedCallback.run(this)\n    }\n\n    disconnectedCallback() {\n      super.disconnectedCallback()\n      return this.__opts.disconnectedCallback.run(this)\n    }\n\n    protected willUpdate(_changedProperties: PropertyValues<this>) {\n      super.willUpdate(_changedProperties)\n      return this.__opts.willUpdate.run(this, _changedProperties)\n    }\n\n    protected shouldUpdate(_changedProperties: PropertyValues<this>): boolean {\n      return (\n        super.shouldUpdate(_changedProperties) || this.__opts.shouldUpdate.run(this, _changedProperties)?.some(Boolean)\n      )\n    }\n\n    protected updated(_changedProperties: PropertyValues<this>) {\n      super.updated(_changedProperties)\n      return this.__opts.updated.run(this, _changedProperties)\n    }\n\n    protected firstUpdated(_changedProperties: PropertyValues<this>) {\n      super.firstUpdated(_changedProperties)\n      return this.__opts.firstUpdated.run(this, _changedProperties)\n    }\n\n    protected performUpdate() {\n      super.performUpdate()\n      return this.__opts.performUpdate.run(this)\n    }\n  }\n  return Mixin as unknown as Result\n}\n\ntype SomeFunction = (...args: unknown[]) => unknown\n\nexport const onConnected = <Type extends LitElement>(cb: SomeFunction) =>\n  getCurrentOptions<InternalOptions<Type>>().connectedCallback.add(cb)\nexport const onDisconnected = <Type extends LitElement>(cb: SomeFunction) =>\n  getCurrentOptions<InternalOptions<Type>>().disconnectedCallback.add(cb)\nexport const onShouldUpdate = <Type extends LitElement>(cb: ChangedPropertiesPredicate<Type>) =>\n  getCurrentOptions<InternalOptions<Type>>().shouldUpdate.add(cb)\nexport const onWillUpdate = <Type extends LitElement>(cb: ChangedPropertiesConsumer<Type>) =>\n  getCurrentOptions<InternalOptions<Type>>().willUpdate.add(cb)\nexport const onPerformUpdate = <Type extends LitElement>(cb: SomeFunction) =>\n  getCurrentOptions<InternalOptions<Type>>().performUpdate.add(cb)\nexport const onUpdate = <Type extends LitElement>(cb: ChangedPropertiesConsumer<Type>) =>\n  getCurrentOptions<InternalOptions<Type>>().update.add(cb)\nexport const onFirstUpdated = <Type extends LitElement>(cb: ChangedPropertiesConsumer<Type>) =>\n  getCurrentOptions<InternalOptions<Type>>().firstUpdated.add(cb)\nexport const onUpdated = <Type extends LitElement>(cb: ChangedPropertiesConsumer<Type>) =>\n  getCurrentOptions<InternalOptions<Type>>().updated.add(cb)\n","import type { CSSResultGroup, PropertyDeclaration } from 'lit'\nimport { LitElement } from 'lit'\nimport { dummyFn } from '../utils/dummyFn'\nimport type { ValidatedCustomElementName, ValidCustomElementName } from '../utils/ValidCustomElementName'\nimport { isFunction, isString, isUndefined } from '../utils/is'\nimport { registerCustomElement } from '../utils/browser'\nimport { withHooks } from './hooks'\nimport { withCurrentInstance } from '../currentInstance'\n\nexport type UnwrapProps<Props extends Record<string, DefinePropertyDeclaration>> = {\n  [K in keyof Props]: InferPropType<Props[K]>\n}\n\ntype TypeConstructor<T = unknown> = (new (...args: unknown[]) => T & {}) | (() => T) | TypeMethod<T>\ntype TypeMethod<T, Ctor = unknown> = [T] extends [((...args: unknown[]) => unknown) | undefined]\n  ? { new (): Ctor; (): T; readonly prototype: Ctor }\n  : never\n\ntype InferPropType<T, NullAsAny = true> = [T] extends [null]\n  ? NullAsAny extends true\n    ? unknown\n    : null\n  : [T] extends [{ type: null | true }]\n    ? unknown\n    : [T] extends [DefinePropertyDeclaration<unknown, ObjectConstructor>]\n      ? Record<string, unknown>\n      : [T] extends [DefinePropertyDeclaration<unknown, BooleanConstructor>]\n        ? boolean\n        : [T] extends [DefinePropertyDeclaration<unknown, DateConstructor>]\n          ? Date\n          : [T] extends [DefinePropertyDeclaration<unknown, (infer U)[]>]\n            ? U extends DateConstructor\n              ? Date | InferPropType<U, false>\n              : InferPropType<U, false>\n            : [T] extends [DefinePropertyDeclaration<unknown, infer V>]\n              ? V extends StringConstructor\n                ? string\n                : V extends NumberConstructor\n                  ? number\n                  : V extends BooleanConstructor\n                    ? boolean\n                    : V extends DateConstructor\n                      ? Date\n                      : V extends PropType<infer P>\n                        ? P\n                        : V\n              : T\n\nexport type PropType<T> = TypeConstructor<T> | (TypeConstructor<T> | null)[]\n\nexport interface DefinePropertyDeclaration<Type = unknown, TypeHint = unknown>\n  extends PropertyDeclaration<Type, TypeHint> {\n  readonly type?: TypeHint\n  readonly default?: TypeHint | (() => TypeHint)\n}\n\nconst assignDefaultValues = <T extends HTMLElement>(obj: T, props?: Record<string, DefinePropertyDeclaration>) =>\n  props &&\n  Object.entries(props).forEach(([key, prop]: [unknown, DefinePropertyDeclaration]) => {\n    if (isUndefined(prop.default)) return\n    const value = (isFunction(prop.default) ? prop.default.call(null) : prop.default) as T[keyof T]\n    obj[key as keyof T] ??= value\n  })\n\nexport const defineElementWithOptions = <\n  Name extends ValidCustomElementName,\n  UseShadowRoot extends boolean,\n  Properties extends Record<string, DefinePropertyDeclaration>,\n  Styles extends UseShadowRoot extends true | undefined ? CSSResultGroup : never,\n  Parent extends typeof LitElement,\n  Instance extends InstanceType<Parent> & UnwrapProps<Properties>,\n  Render extends (this: Instance) => unknown,\n  Setup extends (this: Instance, comp?: Instance) => void | Render,\n>(\n  options: {\n    name?: ValidatedCustomElementName<Name>\n    parent?: Parent\n    styles?: Styles\n    props?: Properties\n\n    register?: boolean\n    shadowRoot?: UseShadowRoot\n\n    setup?: Setup\n    render?: Render\n  },\n  decorator: LitElementDecorator\n): typeof LitElement => {\n  options = Object.create(options) as typeof options\n  const { name, parent: BaseClass = LitElement, register } = options\n  const SuperClass = withHooks(decorator(BaseClass))\n\n  const result = class extends SuperClass {\n    static properties = options.props ?? ({} as Properties)\n    static styles = options.styles ?? undefined\n\n    protected createRenderRoot(): HTMLElement | DocumentFragment {\n      return options.shadowRoot === false ? this : super.createRenderRoot()\n    }\n\n    constructor() {\n      super()\n      withCurrentInstance(this, () => {\n        assignDefaultValues(this, options.props)\n        const setupResult = options.setup?.call(this as unknown as Instance, this as unknown as Instance)\n        this.__opts.render = isFunction(setupResult) //\n          ? setupResult\n          : (options.render ?? super.render.bind(this) ?? dummyFn)\n      })\n    }\n\n    render() {\n      return withCurrentInstance(this, () => this.__opts.render?.call(this))\n    }\n  }\n\n  // register the component\n  if (register != false && name) {\n    registerCustomElement(name as ValidCustomElementName, result)\n  }\n  return result as unknown as typeof LitElement\n}\n\nexport type DefinedComponent = ReturnType<typeof defineElement>\nexport type DefinedComponentInstance = InstanceType<DefinedComponent>\n\ntype LitElementDecorator = (t: typeof LitElement) => typeof t\n\nexport type FunctionComponentOptions = {\n  [K in keyof Parameters<typeof defineElementWithOptions>[0]]: Parameters<typeof defineElementWithOptions>[0][K]\n} & {\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  styles: any\n}\n\nexport const defineFunctionalComponent = <Name extends ValidCustomElementName>(\n  name: ValidatedCustomElementName<Name>,\n  render: () => unknown,\n  opts = {} as FunctionComponentOptions,\n  decorator: LitElementDecorator\n) => defineElementWithOptions({ ...opts, name, render, shadowRoot: false }, decorator)\n\nexport type SkipLastParam<T extends unknown[]> = T extends [...infer U, unknown] ? U : never\n\nexport function defineElement(\n  name: ValidCustomElementName,\n  render: () => unknown,\n  opts?: FunctionComponentOptions\n): typeof LitElement\nexport function defineElement<\n  Name extends ValidCustomElementName,\n  UseShadowRoot extends boolean,\n  Properties extends Record<string, DefinePropertyDeclaration>,\n  Styles extends UseShadowRoot extends true | undefined ? CSSResultGroup | undefined : undefined,\n  Parent extends typeof LitElement,\n  Instance extends InstanceType<Parent> & UnwrapProps<Properties>,\n  Render extends (this: Instance) => unknown,\n  Setup extends (this: Instance, comp?: Instance) => void | Render,\n>(options: {\n  name?: ValidatedCustomElementName<Name>\n  parent?: Parent\n  styles?: Styles\n  props?: Properties\n  register?: boolean\n  shadowRoot?: UseShadowRoot\n  setup?: Setup\n  render?: Render\n}): typeof LitElement\nexport function defineElement(\n  ...args:\n    | [name: ValidCustomElementName, render: () => unknown, opts?: FunctionComponentOptions]\n    | SkipLastParam<Parameters<typeof defineElementWithOptions>>\n) {\n  return isString(args[0])\n    ? defineFunctionalComponent(args[0], args[1] as () => unknown, args[2], (l) => l)\n    : defineElementWithOptions(args[0], (l) => l)\n}\n"],"names":["dummyFn","is","value","type","isFunction","isObject","isString","isUndefined","isSubclassOf","subclass","superclass","registerCustomElement","name","constructor","fnSubscriber","subscribers","fn","t","args","withHooks","Base","Mixin","DEFINE_COMPONENT_OPTIONS_SYMBOL","_changedProperties","onConnected","cb","getCurrentOptions","onDisconnected","onShouldUpdate","onWillUpdate","onPerformUpdate","onUpdate","onFirstUpdated","onUpdated","assignDefaultValues","obj","props","key","prop","defineElementWithOptions","options","decorator","BaseClass","LitElement","register","SuperClass","result","withCurrentInstance","setupResult","defineFunctionalComponent","render","opts","defineElement","l"],"mappings":"gFAEaA,EAAc,IAAsB,CAEjD,ECFMC,EAAK,CAACC,EAAgBC,IAA0B,OAAO,UAAU,SAAS,KAAKD,CAAK,IAAM,WAAWC,CAAI,IAGlGC,EAAcF,GAAgCD,EAAGC,EAAO,UAAU,EAClEG,EAAYH,GAA2CD,EAAGC,EAAO,QAAQ,EACzEI,EAAYJ,GAAoCD,EAAGC,EAAO,QAAQ,EAClEK,EAAeL,GAAuCA,IAAU,OAEhEM,EAAe,CAACC,EAAoBC,IAC/CD,IAAaC,GAAc,OAAO,UAAU,cAAc,KAAKA,EAAW,UAAWD,EAAS,SAAS,ECT5FE,EAAwB,CAACC,EAA8BC,IAClE,eAAe,OAAOD,EAAMC,CAAW,EC4BnCC,EAAe,IAA8C,CACjE,MAAMC,EAAc,CAAA,EACpB,MAAO,CACL,IAAMC,GAAWD,EAAY,KAAKC,CAAE,EACpC,IAAK,CAACC,KAAeC,IAAyBH,EAAY,IAAKC,GAAOA,EAAG,MAAMC,EAAGC,CAAI,CAAC,CAAA,CAE3F,EAIaC,EAOXC,GACW,CACX,MAAeC,UAAcD,CAAK,CAEhC,CAACE,GAA+B,EAA2B,CACzD,OAAQtB,EACR,kBAAmBc,EAAA,EACnB,qBAAsBA,EAAA,EACtB,WAAYA,EAAA,EACZ,cAAeA,EAAA,EACf,aAAcA,EAAA,EACd,OAAQA,EAAA,EACR,aAAcA,EAAA,EACd,QAASA,EAAA,CAAqD,EAGhE,IAAc,QAAS,CACrB,OAAO,KAAKQ,EAAAA,CAA+B,CAC7C,CAE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