{"version":3,"file":"keycloakify-angular-login-services-user-profile-form.mjs","names":[],"sources":["../../src/login/services/user-profile-form/user-profile-form.service.ts"],"sourcesContent":["import { inject, Injectable, type OnDestroy } from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { DomSanitizer, type SafeHtml } from '@angular/platform-browser';\nimport type { I18n } from '@keycloakify/angular/login/i18n';\nimport { LOGIN_I18N } from '@keycloakify/angular/login/tokens/i18n';\nimport { KC_LOGIN_CONTEXT } from '@keycloakify/angular/login/tokens/kc-context';\nimport { DO_MAKE_USER_CONFIRM_PASSWORD } from '@keycloakify/angular/login/tokens/make-user-confirm-password';\nimport type {\n    Attribute,\n    PasswordPolicies,\n    Validators\n} from 'keycloakify/login/KcContext';\nimport * as reactlessApi from 'keycloakify/login/lib/getUserProfileApi';\nimport { BehaviorSubject, map, type Observable } from 'rxjs';\nimport { assert, type Equals } from 'tsafe/assert';\n\nexport { getButtonToDisplayForMultivaluedAttributeField } from 'keycloakify/login/lib/getUserProfileApi';\n\nexport type FormFieldError = {\n    errorMessage: SafeHtml;\n    errorMessageStr: string;\n    source: FormFieldError.Source;\n    fieldIndex: number | undefined;\n};\n\n{\n    type A = Omit<FormFieldError, 'errorMessage' | 'errorMessageStr'>;\n    type B = Omit<reactlessApi.FormFieldError, 'advancedMsgArgs'>;\n\n    assert<Equals<A, B>>();\n}\n\nexport namespace FormFieldError {\n    export type Source =\n        Source.Validator | Source.PasswordPolicy | Source.Server | Source.Other;\n\n    export namespace Source {\n        export type Validator = {\n            type: 'validator';\n            name: keyof Validators;\n        };\n        export type PasswordPolicy = {\n            type: 'passwordPolicy';\n            /**\n             * passwordHistory and forceExpiredPasswordChange can't be validated client side\n             * (they require the stored credentials), they are only informative.\n             */\n            name: Exclude<\n                keyof PasswordPolicies,\n                'passwordHistory' | 'forceExpiredPasswordChange'\n            >;\n        };\n        export type Server = {\n            type: 'server';\n        };\n\n        export type Other = {\n            type: 'other';\n            rule: 'passwordConfirmMatchesPassword' | 'requiredField';\n        };\n    }\n}\n\n{\n    type A = FormFieldError.Source;\n    type B = reactlessApi.FormFieldError.Source;\n\n    assert<Equals<A, B>>();\n}\n\nexport type FormFieldState = {\n    attribute: Attribute;\n    displayableErrors: FormFieldError[];\n    valueOrValues: string | string[];\n};\n\n{\n    type A = Omit<FormFieldState, 'displayableErrors'>;\n    type B = Omit<reactlessApi.FormFieldState, 'displayableErrors'>;\n\n    assert<Equals<A, B>>();\n}\n\nexport type FormState = {\n    isFormSubmittable: boolean;\n    formFieldStates: FormFieldState[];\n};\n\n{\n    type A = Omit<FormState, 'formFieldStates'>;\n    type B = Omit<FormState, 'formFieldStates'>;\n\n    assert<Equals<A, B>>();\n}\n\nexport type FormAction =\n    | {\n          action: 'update';\n          name: string;\n          valueOrValues: string | string[];\n          /** Default false */\n          displayErrorsImmediately?: boolean;\n      }\n    | {\n          action: 'focus lost';\n          name: string;\n          fieldIndex: number | undefined;\n      };\n\n{\n    type A = FormAction;\n    type B = reactlessApi.FormAction;\n\n    assert<Equals<A, B>>();\n}\n\nexport type KcContextLike = reactlessApi.KcContextLike;\n\nexport type I18nLike = Pick<I18n, 'advancedMsgStr'>;\n\n@Injectable({ providedIn: 'root' })\nexport class UserProfileFormService implements OnDestroy {\n    #kcContext = inject<KcContextLike>(KC_LOGIN_CONTEXT);\n    #i18n = inject<I18nLike>(LOGIN_I18N);\n    #doMakeUserConfirmPassword = inject(DO_MAKE_USER_CONFIRM_PASSWORD);\n    #domSanitizer = inject(DomSanitizer);\n    #internal_formState$: BehaviorSubject<reactlessApi.FormState>;\n    #unsubscribe: (() => void) | undefined;\n    public formState$: Observable<FormState>;\n    public dispatchFormAction: (action: FormAction) => void;\n    constructor() {\n        const api = reactlessApi.getUserProfileApi({\n            kcContext: this.#kcContext,\n            doMakeUserConfirmPassword: this.#doMakeUserConfirmPassword\n        });\n        this.#internal_formState$ = new BehaviorSubject<reactlessApi.FormState>(\n            api.getFormState()\n        );\n        this.#unsubscribe = api.subscribeToFormState(() => {\n            this.#internal_formState$.next(api.getFormState());\n        }).unsubscribe;\n        this.formState$ = this.#internal_formState$.asObservable().pipe(\n            takeUntilDestroyed(),\n            map(formState_reactless => ({\n                isFormSubmittable: formState_reactless.isFormSubmittable,\n                formFieldStates: formState_reactless.formFieldStates.map(\n                    formFieldState_reactless => ({\n                        attribute: formFieldState_reactless.attribute,\n                        valueOrValues: formFieldState_reactless.valueOrValues,\n                        displayableErrors: formFieldState_reactless.displayableErrors.map(\n                            formFieldError_reactless => ({\n                                errorMessage: this.#domSanitizer.bypassSecurityTrustHtml(\n                                    this.#i18n.advancedMsgStr(\n                                        ...formFieldError_reactless.advancedMsgArgs\n                                    )\n                                ),\n                                errorMessageStr: this.#i18n.advancedMsgStr(\n                                    ...formFieldError_reactless.advancedMsgArgs\n                                ),\n                                source: formFieldError_reactless.source,\n                                fieldIndex: formFieldError_reactless.fieldIndex\n                            })\n                        )\n                    })\n                )\n            }))\n        );\n        this.dispatchFormAction = api.dispatchFormAction;\n    }\n\n    ngOnDestroy(): void {\n        this.#internal_formState$.complete();\n        this.#internal_formState$.unsubscribe();\n        if (this.#unsubscribe) this.#unsubscribe();\n    }\n}\n"],"mappings":";;;;;;;;;;;AA6BI,OAAM;AAsCN,OAAM;AAaN,OAAM;AAYN,OAAM;AAqBN,OAAM;AAQV,IAAa,yBAAb,MAAa,uBAAsB;CAC/B,aAAa,OAAsB,gBAAgB;CACnD,QAAQ,OAAiB,UAAU;CACnC,6BAA6B,OAAO,6BAA6B;CACjE,gBAAgB,OAAO,YAAY;CACnC;CACA;CAGA,cAAA;EACI,MAAM,MAAM,aAAa,kBAAkB;GACvC,WAAW,KAAK;GAChB,2BAA2B,KAAK;EACnC,CAAA;EACD,KAAK,uBAAuB,IAAI,gBAC5B,IAAI,aAAY,CAAE;EAEtB,KAAK,eAAe,IAAI,2BAA0B;GAC9C,KAAK,qBAAqB,KAAK,IAAI,aAAY,CAAE;EACrD,CAAC,CAAC,CAAC;EACH,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