import snippets from './snippets';
import { MetaType } from '../_utils/type';
import {
  getDataSourceVariables,
  setBatchVariables,
} from '../data-source-form/utils';
import { genCptValuesByDetail } from './utils';

const dataSourceWrapper: MetaType = {
  snippets,
  componentName: 'DataSourceWrapper',
  title: '数据源容器',
  category: '无代码容器',
  group: '无代码组件',
  docUrl: '',
  screenshot: '',
  devMode: 'proCode',
  npm: {
    package: '@pisell/materials',
    version: '1.0.1',
    exportName: 'DataSourceWrapper',
    main: 'src/index.tsx',
    destructuring: true,
    subName: '',
  },
  generalProps: [
    {
      type: 'group',
      title: {
        type: 'i18n',
        'en-US': 'Data',
        'zh-CN': '数据',
      },
      display: 'accordion',
      items: [
        {
          name: 'dataSource',
          title: {
            type: 'i18n',
            'en-US': 'Source',
            'zh-CN': '数据源',
          },
          display: 'block',
          extraProps: {
            setValue(target: any, value: any) {
              if (!value.item) {
                target.getProps().setPropValue('title', '');
                setBatchVariables(target, [
                  {
                    label: 'currentFields',
                    value: [],
                  },
                  {
                    label: 'dataSourceTitle',
                    value: '',
                  },
                  {
                    label: 'dataSourceValue',
                    value: '',
                  },
                  {
                    label: 'cptValuesVariables',
                    value: [],
                  },
                ]);
                target.node.children.importSchema([]);
                return target.getProps().setPropValue('dataSource', '');
              }

              // 如果重新选择选择数据源，则清空内部内容
              if (value.isSelect) {
                target.getProps().setPropValue('title', '');
                target.node.children.importSchema([]);
              }

              const dataSource = value.isVariable ? value.value : value.item;

              setBatchVariables(target, [
                {
                  label: 'currentFields',
                  value: value.item.fields,
                },
                {
                  label: 'dataSourceTitle',
                  value: value.title,
                },
                {
                  label: 'dataSourceValue',
                  value: dataSource,
                },
                {
                  label: 'cptValuesVariables',
                  value: genCptValuesByDetail(dataSource, target),
                },
              ]);


              return target.getProps().setPropValue('dataSource', dataSource);
            },
          },
          setter: {
            componentName: 'PisellDataSourceSetter',
            props: (target: any) => {
              return {
                filters: {
                  tables: 'all',
                  variables: {}
                },
                variables: getDataSourceVariables(target),
              };
            },
          },
        },
      ],
    },
    {
      type: 'group',
      name: 'overrideData',
      title: {
        type: 'i18n',
        'en-US': 'Override Data',
        'zh-CN': '覆盖数据',
      },
      display: 'accordion',
      items: [
        {
          name: 'overrideData.list',
          title: {
            type: 'i18n',
            'en-US': 'List Data',
            'zh-CN': '列表数据',
          },
          setter: 'JsonSetter'
        },
        {
          name: 'overrideData.get',
          title: {
            type: 'i18n',
            'en-US': 'Get Data',
            'zh-CN': '详情数据',
          },
          setter: 'JsonSetter'
        },
      ]
    },
    {
      type: 'group',
      name: 'autoRun',
      title: {
        type: 'i18n',
        'en-US': 'Auto Run',
        'zh-CN': '自动运行',
      },
      display: 'accordion',
      items: [
        {
          name: 'autoRun.list',
          title: {
            type: 'i18n',
            'en-US': 'List Auto Run',
            'zh-CN': '列表自动运行',
          },
          setter: 'BoolSetter',
          defaultValue: false,
        },
        {
          name: 'autoRun.get',
          title: {
            type: 'i18n',
            'en-US': 'Get Auto Run',
            'zh-CN': '详情自动运行',
          },
          setter: 'BoolSetter',
          defaultValue: false,
        }
      ]
    },
    {
      type: 'group',
      name: 'actions',
      title: {
        type: 'i18n',
        'en-US': 'Actions',
        'zh-CN': '操作',
      },
      display: 'accordion',
      items: [
        {
          name: 'actions.list',
          title: {
            type: 'i18n',
            'en-US': 'List Action',
            'zh-CN': '列表操作',
          },
          setter: 'FunctionSetter'
        },
        {
          name: 'actions.get',
          title: {
            type: 'i18n',
            'en-US': 'Get Action',
            'zh-CN': '获取操作',
          },
          setter: 'FunctionSetter'
        },
        {
          name: 'actions.update',
          title: {
            type: 'i18n',
            'en-US': 'Update Action',
            'zh-CN': '更新操作',
          },
          setter: 'FunctionSetter'
        },
        {
          name: 'actions.create',
          title: {
            type: 'i18n',
            'en-US': 'Create Action',
            'zh-CN': '创建操作',
          },
          setter: 'FunctionSetter'
        },
        {
          name: 'actions.destroy',
          title: {
            type: 'i18n',
            'en-US': 'Destroy Action',
            'zh-CN': '删除操作',
          },
          setter: 'FunctionSetter'
        }
      ]
    }
  ],
  configure: {
    component: { isContainer: true },
    supports: {
      style: true,
      events: [],
    },
    advanced: {
      callbacks: {},
    },
  },
};
export default dataSourceWrapper;
