import { __awaiter, __decorate } from "tslib";
import { customElement, observable, volatile } from '@microsoft/fast-element';
import { BaseRoute } from '../base';
import { commonLoginRouteStyles } from '../../styles';
import { ResetPasswordTemplate as template } from './reset-password.template';
import { logger } from '../../utils';
let ResetPassword = class ResetPassword extends BaseRoute {
    constructor() {
        super(...arguments);
        this.hasPasswordExpired = false;
        this.passwordResetToken = '';
    }
    connectedCallback() {
        const _super = Object.create(null, {
            connectedCallback: { get: () => super.connectedCallback }
        });
        return __awaiter(this, void 0, void 0, function* () {
            _super.connectedCallback.call(this);
            yield this.autofill();
        });
    }
    enter(phase) {
        return __awaiter(this, void 0, void 0, function* () {
            this.hasPasswordExpired = !!phase.route.queryParams.expired;
            this.passwordResetToken = phase.route.queryParams.password;
        });
    }
    onSubmit() {
        const _super = Object.create(null, {
            onSubmit: { get: () => super.onSubmit }
        });
        return __awaiter(this, void 0, void 0, function* () {
            yield _super.onSubmit.call(this);
            this.passwordResetToken
                ? yield this.onForgotPasswordWorkflow()
                : yield this.onChangePasswordWorkflow();
            this.isSubmitting = false;
        });
    }
    onForgotPasswordWorkflow() {
        return __awaiter(this, void 0, void 0, function* () {
            logger.debug('forgot password submission');
            const message = this.messageBuilder.createForgotPasswordTokenMessage(this.orgUsername, this.passwordResetToken, this.password2);
            yield this.sendMessage(message, 'Password reset success.', () => {
                this.storeCredentials(this.password2);
            });
        });
    }
    onChangePasswordWorkflow() {
        return __awaiter(this, void 0, void 0, function* () {
            logger.debug('change password submission');
            const message = this.messageBuilder.createChangePasswordMessage(this.orgUsername, this.password, this.password2);
            yield this.sendMessage(message, 'Password change success.', () => {
                this.storeCredentials(this.password2);
            });
        });
    }
    get headingText() {
        return this.hasPasswordExpired ? 'You are required to change your password' : 'Change Password';
    }
};
__decorate([
    observable
], ResetPassword.prototype, "hasPasswordExpired", void 0);
__decorate([
    observable
], ResetPassword.prototype, "passwordResetToken", void 0);
__decorate([
    volatile
], ResetPassword.prototype, "headingText", null);
ResetPassword = __decorate([
    customElement({
        name: 'foundation-login-reset-password',
        template,
        styles: commonLoginRouteStyles,
    })
], ResetPassword);
export { ResetPassword };
//# sourceMappingURL=reset-password.js.map