"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addElementByPath = exports.updateChildPaths = exports.createValue = exports.endWidth = exports.defaultCols = exports.settingKeys = exports.formPlugins = exports.defaultTarget = exports.transformCharMaps = exports.tagNameMaps = exports.characterShortId = void 0;
const isPlainObject_1 = require("lodash/isPlainObject");
const shortid = require('shortid');
const idCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
const characterShortId = (shortId) => {
    shortId.characters(idCharacters);
};
exports.characterShortId = characterShortId;
(0, exports.characterShortId)(shortid);
exports.tagNameMaps = {
    Column: 'Col',
    TextArea: 'Input.TextArea',
    RadioGroup: 'Radio.Group',
    CheckBoxGroup: 'Checkbox.Group',
    HeaderCell: 'Table.Column',
    ListItem: 'List.Item',
};
// xml内需要转义的字符
exports.transformCharMaps = {
    '&': '&amp;',
    '>': '&gt;',
    '<': '&lt;',
};
// 定义一般组件的target
exports.defaultTarget = ['block', 'form', 'table', 'column', 'placeholder', 'if'];
// 定义form组件
exports.formPlugins = [
    'button',
    'input',
    'inputItem',
    'inputNumber',
    'textArea',
    'password',
    'richEditor',
    'select',
    'cascader',
    'radio',
    'picker',
    'radioGroup',
    'checkBoxGroup',
    'timePicker',
    'datePicker',
    'rangePicker',
    'switch',
    'slider',
    'rate',
    'upload',
    'popEdit',
    'imageUpload',
    'tree',
    'treeSelect',
    'participant',
    'hyperlink',
];
// 列举元素设置key
exports.settingKeys = ['formSettings', 'settings', 'formItemSettings', 'tableSettings', 'columnSettings', 'componentSettings', 'blockSettings'];
exports.defaultCols = { span: 24, offset: 0 };
const endWidth = (regStr, str) => {
    const reg = new RegExp(`${regStr}$`);
    return reg.test(str);
};
exports.endWidth = endWidth;
const createValue = (text, typography = 'body1') => {
    return createDocument(createTextLeaf(text), typography);
};
exports.createValue = createValue;
const createTextLeaf = (text) => {
    return { object: 'leaf', text };
};
const createDocument = (leaf, typography = 'body1') => {
    return {
        object: 'value',
        document: {
            object: 'document',
            nodes: [
                {
                    object: 'block',
                    type: typography,
                    nodes: [
                        {
                            object: 'text',
                            leaves: [leaf],
                        },
                    ],
                },
            ],
        },
    };
};
const updateChildPaths = (element) => {
    if (!element.id) {
        element.id = shortid.generate();
    }
    if (!element.path) {
        element.path = '0';
    }
    if (Array.isArray(element.elements)) {
        // Process children only if "elements" is an array of objects.
        // We may get an array of strings when working with shallow element copies.
        if ((0, isPlainObject_1.default)(element.elements[0])) {
            element.elements.forEach((el, index) => {
                if (!el.id) {
                    el.id = shortid.generate();
                }
                el.path = `${element.path}.${index}`;
                if (el.elements && el.elements.length) {
                    (0, exports.updateChildPaths)(el);
                }
            });
        }
    }
};
exports.updateChildPaths = updateChildPaths;
// 根据path路径添加元素
const addElementByPath = (element, parent, path) => {
    if (path.length <= 0) {
        return parent;
    }
    let newParent;
    const pathArr = path.split('.');
    pathArr.splice(0, 1);
    pathArr.forEach((path, index) => {
        const num = Number(path);
        if (index === pathArr.length - 1) {
            if (newParent) {
                if (!newParent.elements) {
                    newParent.elements = [];
                }
                const { elements } = newParent;
                elements.splice(num, 0, element);
            }
        }
        else if (!newParent) {
            newParent = parent.elements[num];
        }
        else {
            newParent = newParent.elements[num];
        }
    });
    return parent;
};
exports.addElementByPath = addElementByPath;
