{"version":3,"file":"qualcomm-ui-angular-core-state.mjs","sources":["../../src/state/controlled-state.service.ts","../../src/state/controlled-state-legacy.ts","../../src/state/use-controlled-state.ts","../../src/state/qualcomm-ui-angular-core-state.ts"],"sourcesContent":["// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.\n// SPDX-License-Identifier: BSD-3-Clause-Clear\n\nimport type {SimpleChange} from \"@angular/core\"\nimport {Subject} from \"rxjs\"\n\nimport {defined} from \"@qualcomm-ui/utils/guard\"\n\nexport class ControlledStateService<T, OnChangeArg = Event> {\n  readonly valueChanged = new Subject<T | undefined>()\n\n  protected onChange?: (value: T, event?: OnChangeArg) => void\n  protected value: T | undefined\n  protected initialized = false\n  protected name: string\n  protected state: string\n  protected controlledValue: T | undefined\n\n  get controlled() {\n    return this._controlled\n  }\n  protected _controlled: boolean = false\n\n  init(\n    /**\n     * The controlled value.\n     */\n    controlledValue: T | undefined,\n    /**\n     * The default value.\n     */\n    defaultValue: T | undefined,\n    /**\n     * onChange function fired when the state is modified internally. If the\n     * controlled state is modified from outside of this service, this function will\n     * not be called.\n     */\n    onChange: (value: T, event?: OnChangeArg) => void,\n    /**\n     * The name of this instance. Used in error logging.\n     */\n    name: string,\n    /**\n     * The name of the tracked property. Used in error logging.\n     */\n    state: string,\n  ) {\n    if (this.initialized) {\n      throw new Error(\"Cannot initialize controlled state more than once.\")\n    }\n    this._controlled = defined(controlledValue)\n    this.onChange = onChange\n    this.controlledValue = controlledValue\n    this.value = defaultValue\n    this.name = name\n    this.state = state\n    this.initialized = true\n  }\n\n  set(value: T, event?: OnChangeArg) {\n    if (!this._controlled) {\n      this.value = value\n      this.valueChanged.next(value)\n    }\n    this.onChange?.(value, event)\n  }\n\n  get() {\n    return this.controlledValue ?? this.value\n  }\n\n  private emitWarning() {\n    console.error(\n      [\n        `QUI: A component is changing the ${\n          this.controlled ? \"\" : \"un\"\n        }controlled ${this.state} state of ${this.name} to be ${\n          this.controlled ? \"un\" : \"\"\n        }controlled.`,\n        \"Elements should not switch from uncontrolled to controlled (or vice versa).\",\n        `Decide between using a controlled or uncontrolled ${this.name} ` +\n          \"element for the lifetime of the component.\",\n        \"The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.\",\n      ].join(\"\\n\"),\n    )\n  }\n\n  handleOnChange(change?: SimpleChange) {\n    if (!change) {\n      return\n    }\n    if (\n      !this.controlled &&\n      defined(change.currentValue) &&\n      !change.isFirstChange()\n    ) {\n      this.emitWarning()\n    }\n    const value = change.currentValue\n    this.controlledValue = value\n    this.valueChanged.next(value)\n  }\n}\n","// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.\n// SPDX-License-Identifier: BSD-3-Clause-Clear\n\nimport {\n  computed,\n  type OutputEmitterRef,\n  signal,\n  type SimpleChange,\n  untracked,\n} from \"@angular/core\"\n\nimport {defined} from \"@qualcomm-ui/utils/guard\"\n\nexport class ControlledStateLegacy<\n  T,\n  OnChangeParams extends Record<string, any> | null = null,\n> {\n  get controlled() {\n    return this._controlled.asReadonly()\n  }\n  protected readonly value = signal<T | undefined>(undefined)\n  protected readonly controlledValue = signal<T | undefined>(undefined)\n  private readonly _controlled = signal<boolean>(false)\n\n  constructor(\n    /**\n     * The controlled value.\n     */\n    controlledValue: T | undefined,\n    /**\n     * The default value.\n     */\n    defaultValue: T | undefined,\n    /**\n     * onChange function fired when the state is modified internally. If the\n     * controlled state is modified from outside of this service, this function will\n     * not be called.\n     */\n    private onChange: OutputEmitterRef<any>,\n    /**\n     * The name of this instance. Used in error logging.\n     */\n    private name: string,\n    /**\n     * The name of the tracked property. Used in error logging.\n     */\n    private state: string,\n  ) {\n    this._controlled.set(defined(controlledValue))\n    this.controlledValue.set(controlledValue)\n    this.value.set(defaultValue)\n  }\n\n  set(value: T, options?: OnChangeParams) {\n    if (!this._controlled()) {\n      // if not controlled, we use the internal value.\n      untracked(() => this.value.set(value))\n    }\n    this.onChange.emit({...options, index: value})\n  }\n\n  readonly get = computed(() => {\n    const controlledValue = this.controlledValue()\n    const value = this.value()\n    return controlledValue ?? value\n  })\n\n  private emitWarning() {\n    console.error(\n      [\n        `QUI: A component is changing the ${\n          this.controlled() ? \"\" : \"un\"\n        }controlled ${this.state} state of ${this.name} to be ${\n          this.controlled() ? \"un\" : \"\"\n        }controlled.`,\n        \"Elements should not switch from uncontrolled to controlled (or vice versa).\",\n        `Decide between using a controlled or uncontrolled ${this.name} ` +\n          \"element for the lifetime of the component.\",\n        \"The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.\",\n      ].join(\"\\n\"),\n    )\n  }\n\n  handleOnChange(change?: SimpleChange) {\n    if (!change) {\n      return\n    }\n    if (\n      !this.controlled() &&\n      defined(change.currentValue) &&\n      !change.isFirstChange()\n    ) {\n      this.emitWarning()\n    }\n    const value = change.currentValue\n    this.controlledValue.set(value)\n    this.value.set(value)\n  }\n}\n","// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.\n// SPDX-License-Identifier: BSD-3-Clause-Clear\n\nimport {computed, signal, type Signal} from \"@angular/core\"\n\ninterface ControlledStateOptions<T> {\n  defaultValue?: Signal<T | undefined> | T | undefined\n  onChange?: ((value: T, ...rest: any[]) => void) | undefined\n  value?: Signal<T | undefined>\n}\n\nexport interface ControlledState<T> {\n  setValue: (newValue: T, ...rest: any[]) => void\n  value: Signal<T | undefined>\n}\n\nexport function useControlledState<T>(\n  options: ControlledStateOptions<T>,\n): Signal<ControlledState<T>> {\n  const {defaultValue, onChange, value: controlledValue} = options\n\n  return computed(() => {\n    const isControlled = controlledValue?.() !== undefined\n    const localValue = signal<T | undefined>(access(defaultValue))\n\n    const value = isControlled ? controlledValue : localValue.asReadonly()\n\n    const setValue = (newValue: T, ...rest: any[]) => {\n      onChange?.(newValue, ...rest)\n      if (!isControlled) {\n        localValue.set(newValue)\n      }\n    }\n\n    return {setValue, value} as ControlledState<T>\n  })\n}\n\nfunction access<T>(value: Signal<T> | T | undefined): T | undefined {\n  if (value === undefined) {\n    return undefined\n  }\n  if (typeof value === \"function\") {\n    return (value as unknown as Signal<T>)()\n  }\n  return value as T\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;AAAA;AACA;MAOa,sBAAsB,CAAA;AACxB,IAAA,YAAY,GAAG,IAAI,OAAO,EAAiB;AAE1C,IAAA,QAAQ;AACR,IAAA,KAAK;IACL,WAAW,GAAG,KAAK;AACnB,IAAA,IAAI;AACJ,IAAA,KAAK;AACL,IAAA,eAAe;AAEzB,IAAA,IAAI,UAAU,GAAA;QACZ,OAAO,IAAI,CAAC,WAAW;IACzB;IACU,WAAW,GAAY,KAAK;IAEtC,IAAI;AACF;;AAEG;IACH,eAA8B;AAC9B;;AAEG;IACH,YAA2B;AAC3B;;;;AAIG;IACH,QAAiD;AACjD;;AAEG;IACH,IAAY;AACZ;;AAEG;IACH,KAAa,EAAA;AAEb,QAAA,IAAI,IAAI,CAAC,WAAW,EAAE;AACpB,YAAA,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC;QACvE;AACA,QAAA,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,eAAe,CAAC;AAC3C,QAAA,IAAI,CAAC,QAAQ,GAAG,QAAQ;AACxB,QAAA,IAAI,CAAC,eAAe,GAAG,eAAe;AACtC,QAAA,IAAI,CAAC,KAAK,GAAG,YAAY;AACzB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI;AAChB,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,WAAW,GAAG,IAAI;IACzB;IAEA,GAAG,CAAC,KAAQ,EAAE,KAAmB,EAAA;AAC/B,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;AACrB,YAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,YAAA,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC;QAC/B;QACA,IAAI,CAAC,QAAQ,GAAG,KAAK,EAAE,KAAK,CAAC;IAC/B;IAEA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