"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDomEntities = void 0;
const fs = require("fs");
const path = require("path");
const cheerio = require('cheerio');
let $;
// let $DS;
const _getXml = uri => {
    let _xmlDoc;
    try {
        const xml = fs.readFileSync(uri, 'utf8');
        _xmlDoc = cheerio.load(xml, {
            xml: {
                xmlMode: true,
                decodeEntities: false,
            },
        });
    }
    catch (_a) {
        _xmlDoc = cheerio.load('<InvalidDocument />', {
            xml: {
                xmlMode: true,
                decodeEntities: false,
            },
        });
    }
    return _xmlDoc;
};
// const _getRecordDatas = ( name: string)  => {
//     const $records = $DS(`Records[object=${name}] Record`);
//     const ds: any = [];
//     $records.each((i, v) => {
//         const record: any = {};
//         $DS(v).children().each((idx, ele) => {
//             record[ele.name] = $DS(ele).text();
//         });
//         ds.push(record);
//     })
//     return ds;
// }
/**
 * 获取对象的isDisplayName 字段 如果没有默认是ID字段
 * @param entity
 */
// const _getEntitiesDisplayName = entity => {
//   const $displayName = entity.children('Field[isDisplayName=true]');
//   if ($displayName.length > 0) {
//     return $displayName.attr('name');
//   }
//   return 'id';
// };
const getDomEntities = function (pathName) {
    const domUri = path.join(pathName, 'dom.xml');
    // const dataSamplesUri = path.join(`${pathName}/Data`, 'Objects', 'sample-data.xml');
    $ = _getXml(domUri);
    // $DS = _getXml(dataSamplesUri);
    const entities = $('Object')
        .toArray()
        .map(v => {
        const $entity = $(v);
        const entity = Object.assign({}, $entity.attr());
        // const fields = $entity.children('Field').toArray().map((ele: any) => $(ele).attr());
        const fields = $entity
            .children('Field')
            .toArray()
            .map(ele => {
            const $filed = $(ele);
            const filed = Object.assign({}, $filed.attr());
            // 当前节点下有Options
            const options = $filed.children('Options').toArray();
            if (options.length > 0) {
                const $option = $(options[0]);
                const refId = $(options[0]).attr('ref-id');
                const $refOption = $(`Options[id=${refId}]`);
                if (refId && refId !== '') {
                    const option = $('Collection')
                        .children(`Options[id=${refId}]`)
                        .children('Option')
                        .toArray()
                        .map(opt => $(opt).attr());
                    filed.optionsSource = option;
                    filed.options = refId;
                }
                else {
                    const option = $option
                        .children('Option')
                        .toArray()
                        .map(opt => $(opt).attr());
                    filed.options = option;
                    filed.optionsSource = option;
                }
                if ($filed.attr('type') === 'Picklist') {
                    filed.type = $refOption.attr('type');
                    filed.displayControl = 'select';
                }
                else if ($filed.attr('type') === 'Picklist(Multi-Select)') {
                    filed.type = $refOption.attr('type');
                    filed.displayControl = 'multiselect';
                }
            }
            // 查找当前是否时外键
            const $NavigationField = $entity.children(`NavigationField[field=${filed.name}]`);
            if ($NavigationField.length > 0) {
                filed.type = $NavigationField.attr('type');
                filed.forwardName = $NavigationField.attr('name');
                filed.reverseName = $NavigationField.attr('partner');
                // //外键类型 option为关联表 Sample Data
                // let optionsRecordDate = _getRecordDatas(filed.type);
                // const isDisplayName = _getEntitiesDisplayName($(`Object[name=${filed.type}]`));
                // optionsRecordDate = optionsRecordDate.map((v: any) => {
                //     const option = {index: v.id, value: v.id, label: v[isDisplayName]};
                //     return option;
                // })
                // filed.options = optionsRecordDate;
                // filed.optionsSource = optionsRecordDate;
            }
            return filed;
        });
        entity.fields = fields;
        // 查询 sampleData
        // let sampleData = _getRecordDatas(entity.name);
        // entity.sampleData = sampleData;
        return entity;
    });
    return entities;
};
exports.getDomEntities = getDomEntities;
