/*****************************************************************************\
|                                               ( )_  _                       |
|    _ _    _ __   _ _    __    ___ ___     _ _ | ,_)(_)  ___   ___     _     |
|   ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| |  | |/',__)/' v `\ /'_`\   |
|   | (_) )| |  ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) )  |
|   | ,__/'(_)  `\__,_)`\__  |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/'  |
|   | |                ( )_) |                                                |
|   (_)                 \___/'                                                |
|                                                                             |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved.             |
| Licensed under the AGPL-3.0.                                                |
|                                                                             |
| According to our dual licensing model, this program can be used either      |
| under the terms of the GNU Affero General Public License, version 3,        |
| or under a proprietary license.                                             |
|                                                                             |
| The texts of the GNU Affero General Public License with an additional       |
| permission and of our proprietary license can be found at and               |
| in the LICENSE file you have received along with this program.              |
|                                                                             |
| This program is distributed in the hope that it will be useful,             |
| but WITHOUT ANY WARRANTY, without even the implied warranty of              |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                |
| GNU Affero General Public License for more details.                         |
|                                                                             |
| "General Bots" is a registered trademark of Pragmatismo.io.                 |
| The licensing of the program under the AGPLv3 does not imply a              |
| trademark license. Therefore any rights, title and interest in              |
| our trademarks remain entirely with us.                                     |
|                                                                             |
\*****************************************************************************/
/**
 * @fileoverview Main dialog for kb.gbapp
 */
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProfileDialog = void 0;
const botlib_1 = require("botlib");
const GBAdminService_1 = require("../../admin.gbapp/services/GBAdminService");
const strings_1 = require("../strings");
const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
const phone = require('phone');
/**
 * Dialogs for handling Menu control.
 */
class ProfileDialog extends botlib_1.IGBDialog {
    static getNameDialog(min) {
        return {
            id: '/profile_name', waterfall: [
                async (step) => {
                    if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
                        return await step.beginDialog('/auth');
                    }
                    else {
                        return await step.next(step.options);
                    }
                },
                async (step) => {
                    step.activeDialog.state.options = step.options;
                    const locale = step.context.activity.locale;
                    await step.prompt('textPrompt', strings_1.Messages[locale].whats_name);
                },
                async (step) => {
                    const locale = step.context.activity.locale;
                    const fullName = (text) => {
                        return text.match(/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/gi);
                    };
                    const value = fullName(step.result);
                    if (value === null) {
                        await step.context.sendActivity(strings_1.Messages[locale].validation_enter_name);
                        await step.replaceDialog('/profile_name', step.activeDialog.state.options);
                    }
                    else {
                        step.activeDialog.state.options.name = value[0];
                        return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
                    }
                }
            ]
        };
    }
    static getMobileDialog(min) {
        return {
            id: '/profile_mobile', waterfall: [
                async (step) => {
                    if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
                        return await step.beginDialog('/auth');
                    }
                    else {
                        return await step.next(step.options);
                    }
                },
                async (step) => {
                    step.activeDialog.state.options = step.options;
                    const locale = step.context.activity.locale;
                    await step.prompt('textPrompt', strings_1.Messages[locale].whats_mobile);
                },
                async (step) => {
                    const locale = step.context.activity.locale;
                    let phoneNumber;
                    try {
                        phoneNumber = phone(step.result, 'BRA')[0]; // TODO: Use accordingly to the person.
                        phoneNumber = phoneUtil.parse(phoneNumber);
                    }
                    catch (error) {
                        await step.context.sendActivity(strings_1.Messages[locale].validation_enter_valid_mobile);
                        return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
                    }
                    if (!phoneUtil.isPossibleNumber(phoneNumber)) {
                        await step.context.sendActivity(strings_1.Messages[locale].validation_enter_valid_mobile);
                        return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
                    }
                    else {
                        step.activeDialog.state.options.mobile = `${phoneNumber.values_['1']}${phoneNumber.values_['2']}`;
                        step.activeDialog.state.options.mobileCode = GBAdminService_1.GBAdminService.getMobileCode();
                        return await step.replaceDialog('/profile_mobile_confirm', step.activeDialog.state.options);
                    }
                }
            ]
        };
    }
    static getMobileConfirmDialog(min) {
        return {
            id: '/profile_mobile_confirm', waterfall: [
                async (step) => {
                    if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
                        return await step.beginDialog('/auth');
                    }
                    else {
                        return await step.next(step.options);
                    }
                },
                async (step) => {
                    step.activeDialog.state.options = step.options;
                    const locale = step.context.activity.locale;
                    const from = step.activeDialog.state.options.mobile;
                    if (min.whatsAppDirectLine) {
                        await min.whatsAppDirectLine.sendToDevice(from, `${step.activeDialog.state.options.mobileCode} is your General Bots creation code.`);
                    }
                    else {
                        botlib_1.GBLog.info(`WhatsApp not configured. Here is the code: ${step.activeDialog.state.options.mobileCode}.`);
                    }
                    await step.prompt('textPrompt', strings_1.Messages[locale].confirm_mobile);
                },
                async (step) => {
                    const locale = step.context.activity.locale;
                    if (step.result !== step.activeDialog.state.options.mobileCode) {
                        await step.context.sendActivity(strings_1.Messages[locale].confirm_mobile_again);
                        return await step.replaceDialog('/profile_mobile_confirm', step.activeDialog.state.options);
                    }
                    else {
                        await step.replaceDialog('/profile_email', step.activeDialog.state.options);
                    }
                }
            ]
        };
    }
    static getEmailDialog(min) {
        return {
            id: '/profile_email', waterfall: [
                async (step) => {
                    if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
                        return await step.beginDialog('/auth');
                    }
                    else {
                        return await step.next(step.options);
                    }
                },
                async (step) => {
                    const locale = step.context.activity.locale;
                    await step.prompt('textPrompt', strings_1.Messages[locale].whats_email);
                },
                async (step) => {
                    const locale = step.context.activity.locale;
                    const extractEntity = (text) => {
                        return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
                    };
                    const value = extractEntity(step.context.activity.originalText);
                    if (value === null) {
                        await step.context.sendActivity(strings_1.Messages[locale].validation_enter_valid_email);
                        await step.replaceDialog('/profile_email', step.activeDialog.state.options);
                    }
                    else {
                        step.activeDialog.state.options.email = value[0];
                        await step.replaceDialog(`/${step.activeDialog.state.options.nextDialog}`, step.activeDialog.state.options);
                    }
                }
            ]
        };
    }
}
exports.ProfileDialog = ProfileDialog;
//# sourceMappingURL=ProfileDialog.js.map