typings/lib2/messagev2/common/messageUtil.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageUtil = void 0;
const internal_1 = require("../internal");
/**
 * Utility class for message deserialization
 *
 * FOR INTERNAL USE ONLY
 */
class MessageUtil {
    static deserializeMessage(json) {
        const type = json.type;
        let msg;
        switch (type) {
            case 'attachment':
                msg = Object.assign(new internal_1.AttachmentMessage(null), json);
                break;
            case 'card':
                msg = Object.assign(new internal_1.CardMessage(), json);
                break;
            case 'command':
                msg = Object.assign(new internal_1.CommandMessage(null), json);
                break;
            case 'editForm':
                msg = Object.assign(new internal_1.EditFormMessage(), json);
                break;
            case 'form':
                msg = Object.assign(new internal_1.FormMessage(), json);
                break;
            case 'formSubmission':
                msg = Object.assign(new internal_1.FormSubmissionMessage(), json);
                break;
            case 'location':
                msg = Object.assign(new internal_1.LocationMessage(null), json);
                break;
            case 'postback':
                msg = Object.assign(new internal_1.PostbackMessage(), json);
                break;
            case 'tableForm':
                msg = Object.assign(new internal_1.TableFormMessage(), json);
                break;
            case 'table':
                msg = Object.assign(new internal_1.TableMessage(), json);
                break;
            case 'text':
                msg = Object.assign(new internal_1.TextMessage(null), json);
                break;
            case 'textStream':
                msg = Object.assign(new internal_1.TextStreamMessage(null, null, null, null), json);
                break;
            case 'updateApplicationContextCommand':
                msg = Object.assign(new internal_1.UpdateApplicationContextCommandMessage(null), json);
                break;
            case 'executeApplicationActionCommand':
                msg = Object.assign(new internal_1.ExecuteApplicationActionCommandMessage(null, null), json);
                break;
            default:
                throw new Error(`Error deserializing message, unknown message type: ${type}`);
        }
        msg.deserializeNestedProperties();
        return msg;
    }
    static deserializeActions(json) {
        let actions = [];
        json.forEach(a => actions.push(MessageUtil.deserializeAction(a)));
        return actions;
    }
    static deserializeAction(json) {
        const type = json.type;
        let action;
        switch (type) {
            case 'call':
                action = Object.assign(new internal_1.CallAction(null, null), json);
                break;
            case 'location':
                action = Object.assign(new internal_1.LocationAction(null), json);
                break;
            case 'postback':
                action = Object.assign(new internal_1.PostbackAction(null, null), json);
                break;
            case 'share':
                action = Object.assign(new internal_1.ShareAction(null), json);
                break;
            case 'submitForm':
                action = Object.assign(new internal_1.SubmitFormAction(null), json);
                break;
            case 'popup':
                action = Object.assign(new internal_1.PopupAction(null, null), json);
                break;
            case 'url':
                action = Object.assign(new internal_1.UrlAction(null, null), json);
                break;
            default:
                throw new Error(`Error deserializing action, unknown action type: ${type}`);
        }
        action.deserializeNestedProperties();
        return action;
    }
    static deserializeKeywords(json) {
        let keywords = [];
        json.forEach(keyword => keywords.push(Object.assign(new internal_1.Keyword(null), keyword)));
        return keywords;
    }
    static deserializeSelectFieldOptions(json) {
        let options = [];
        json.forEach(option => options.push(Object.assign(new internal_1.SelectFieldOption(null), option)));
        return options;
    }
    static deserializeCards(json) {
        let cards = [];
        json.forEach(card => {
            let cardInstance = Object.assign(new internal_1.Card(null), card);
            cardInstance.deserializeNestedProperties();
            cards.push(cardInstance);
        });
        return cards;
    }
    static deserializeReadOnlyForms(json) {
        let forms = [];
        json.forEach(form => {
            forms.push(this.deserializeReadOnlyForm(form));
        });
        return forms;
    }
    static deserializeReadOnlyForm(form) {
        let formInstance = Object.assign(new internal_1.ReadOnlyForm(), form);
        formInstance.deserializeNestedProperties();
        return formInstance;
    }
    static deserializeVoice(json) {
        return Object.assign(new internal_1.Voice(null), json);
    }
    static deserializeAttachment(json) {
        return Object.assign(new internal_1.Attachment(null, null), json);
    }
    static deserializeLocation(json) {
        return Object.assign(new internal_1.Location(null, null), json);
    }
    static deserializeChannelExtensions(json) {
        let extensions = Object.assign(new internal_1.ChannelExtensions(), json);
        for (const [key, value] of Object.entries(extensions)) {
            let valueMap = Object.assign(new Map(), value);
            extensions[key] = valueMap;
        }
        ;
        return extensions;
    }
    static deserializePaginationInfo(json) {
        return Object.assign(new internal_1.PaginationInfo(null, null, null), json);
    }
    static deserializeTableHeadings(json) {
        let headings = [];
        json.forEach(row => {
            let instance = Object.assign(new internal_1.TableHeading(null), row);
            instance.deserializeNestedProperties();
            headings.push(instance);
        });
        return headings;
    }
    static deserializeRows(json) {
        let rows = [];
        json.forEach(row => {
            let rowInstance = Object.assign(new internal_1.Row(), row);
            rowInstance.deserializeNestedProperties();
            rows.push(rowInstance);
        });
        return rows;
    }
    static deserializeFormRows(json) {
        let formRows = [];
        json.forEach(formRow => {
            let instance = Object.assign(new internal_1.FormRow(), formRow);
            instance.deserializeNestedProperties();
            formRows.push(instance);
        });
        return formRows;
    }
    static deserializeColumns(json) {
        let columns = [];
        json.forEach(column => {
            let instance = Object.assign(new internal_1.Column(), column);
            instance.deserializeNestedProperties();
            columns.push(instance);
        });
        return columns;
    }
    static deserializeFields(json) {
        let fields = [];
        json.forEach(f => fields.push(MessageUtil.deserializeField(f)));
        return fields;
    }
    static deserializeField(json) {
        const type = json.displayType;
        let field;
        switch (type) {
            case 'datePicker':
                field = Object.assign(new internal_1.DatePickerField(null, null), json);
                break;
            case 'link':
                field = Object.assign(new internal_1.LinkField(null, null), json);
                break;
            case 'multiSelect':
                field = Object.assign(new internal_1.MultiSelectField(null, null), json);
                break;
            case 'numberInput':
                field = Object.assign(new internal_1.NumberInputField(null, null), json);
                break;
            case 'singleSelect':
                field = Object.assign(new internal_1.SingleSelectField(null, null), json);
                break;
            case 'text':
                field = Object.assign(new internal_1.TextField(null, null), json);
                break;
            case 'textInput':
                field = Object.assign(new internal_1.TextInputField(null, null), json);
                break;
            case 'timePicker':
                field = Object.assign(new internal_1.TimePickerField(null, null), json);
                break;
            case 'toggle':
                field = Object.assign(new internal_1.ToggleField(null, null, null, null), json);
                break;
            case 'action':
                field = Object.assign(new internal_1.ActionField(null), json);
                break;
            case 'media':
                field = Object.assign(new internal_1.MediaField(null, null, null), json);
                break;
            default:
                throw new Error(`Error deserializing field, unknown field displayType: ${type}`);
        }
        field.deserializeNestedProperties();
        return field;
    }
}
exports.MessageUtil = MessageUtil;