import { genDataSourceValueKey } from '../data-source-table/utils';

// 根据数据源详情生成Cpt的value变量值
export const genCptValuesByDetail = (item: any, target: any) => {
  const { actions, fields } = item;

  const values = [];

  if (actions.get) {
    values.push({
      label: 'get',
      value: 'get',
      selectable: false,
      children: [
        {
          label: '数据',
          value: 'data',
          selectable: false,
          children: fields.map((field: any) => {
            return {
              label: field.uiSchema.title,
              value: genDataSourceValueKey(target, `get.data.${field.name}`),
            };
          }),
        },
      ],
    });
  }

  if (actions.list) {
    values.push({
      label: 'list',
      value: 'list',
      selectable: false,
      children: [
        {
          label: '数据',
          value: genDataSourceValueKey(target, 'list.data'),
        },
      ],
    });
  }

  return values;
};
