import { merge } from '../utils';
import { Widget, IFileDataSource } from './types';
export const brand: Array<Widget> = [
  {
    name: 'logo',
    type: 'Logo',
    region: 'brand',
    props: {
      text: 'Tj'
    }
  },
  {
    name: 'project',
    type: 'ProjectSwitcher',
    region: 'brand',
    props: {
      link: '/'
    }
  }
];

export const toolbar: Array<Widget> = [
  {
    name: 'toolbar',
    type: 'Toolbar',
    region: 'toolbar'
  }
];

export const action: Array<Widget> = [
  {
    name: 'actions',
    type: 'Actions',
    region: 'action'
  }
];

export const workspace: Array<Widget> = [
  {
    name: 'designer',
    type: 'Designer',
    region: 'workspace',
    disabled: false,
    order: 0,
    trigger: {
      label: '设计视图',
      type: 'tab'
    }
  },
  {
    name: 'schema',
    type: 'Schema',
    region: 'workspace',
    disabled: false,
    order: 1,
    trigger: {
      label: 'Schema',
      type: 'tab',
      closable: true
    }
  },
  {
    name: 'code',
    type: 'Empty',
    region: 'workspace',
    disabled: false,
    order: 2,
    trigger: {
      label: '代码预览',
      type: 'tab',
      closable: true
    }
  },
  {
    name: 'terminal',
    type: 'Empty',
    region: 'workspace',
    disabled: false,
    order: 2,
    trigger: {
      label: '终端输出',
      type: 'tab',
      closable: true
    }
  },
  {
    name: 'market',
    type: 'Empty',
    region: 'workspace',
    disabled: false,
    order: 2,
    trigger: {
      label: '物料市场',
      type: 'tab',
      closable: true
    }
  },
  {
    name: 'docs',
    type: 'Empty',
    region: 'workspace',
    disabled: false,
    order: 2,
    trigger: {
      label: '文档示例',
      type: 'tab',
      closable: true
    }
  }
];

export const apps: Array<Widget> = [
  {
    name: 'files',
    type: 'Files',
    region: 'apps',
    trigger: {
      icon: 'vtj-icon-document',
      label: '文件',
      type: 'panel'
    }
  },
  {
    name: 'components',
    type: 'Components',
    region: 'apps',
    trigger: {
      icon: 'vtj-icon-components',
      label: '组件',
      type: 'panel'
    },
    props: {
      title: '组件库'
    }
  },
  {
    name: 'templates',
    type: 'Templates',
    region: 'apps',
    trigger: {
      icon: 'vtj-icon-template',
      label: '模板',
      type: 'panel'
    }
  },
  {
    name: 'api',
    type: 'Empty',
    region: 'apps',
    trigger: {
      icon: 'vtj-icon-api',
      label: '服务API',
      type: 'panel'
    }
  },

  {
    name: 'dataCube',
    type: 'Empty',
    region: 'apps',
    disabled: true,
    trigger: {
      icon: 'vtj-icon-block',
      label: '数据魔方',
      type: 'panel'
    }
  },
  {
    name: 'link1',
    type: 'Link',
    region: 'apps',
    props: {
      url: 'https://www.baidu.com'
    },
    trigger: {
      icon: 'vtj-icon-user',
      label: '链接1',
      type: 'link'
    }
  }
];

export const setting: Array<Widget> = [
  {
    name: 'properties',
    type: 'Properties',
    region: 'setting',
    trigger: {
      label: '属性',
      type: 'tab'
    }
  },
  {
    name: 'styles',
    type: 'Styles',
    region: 'setting',
    trigger: {
      label: '样式',
      type: 'tab'
    }
  },
  {
    name: 'events',
    type: 'Events',
    region: 'setting',
    trigger: {
      label: '事件',
      type: 'tab'
    }
  },
  {
    name: 'directives',
    type: 'Directives',
    region: 'setting',
    trigger: {
      label: '指令',
      type: 'tab'
    }
  }
];

export interface ISetterConfig {
  name: string;
  component: any;
  dataType: string;
}

export interface IWorkersConfig {
  editorWorker: new () => Worker;
  jsonWorker: new () => Worker;
  cssWorker: new () => Worker;
  htmlWorker: new () => Worker;
  tsWorker: new () => Worker;
}

export interface IHandlersConfig {
  // 数据源工厂函数
  dataSourceFactory?: (
    schema: IFileDataSource
  ) => (params?: any) => Promise<any>;

  //Vue 全局对象
  globalProperties?: Record<string, any>;
}

const _config = {
  activeApp: 'files',
  // 路由前缀
  routePrefix: '/',
  widgets: [...brand, ...toolbar, ...action, ...workspace, ...apps, ...setting],
  setters: [] as ISetterConfig[],
  workers: {} as IWorkersConfig
};

export interface IConfig {
  activeApp?: string;
  routePrefix?: string;
  widgets?: Array<Widget>;
  setters?: ISetterConfig[];
  workers?: IWorkersConfig;
  handlers?: IHandlersConfig;
}

function mergeWidgets(widgets: Widget[] = []) {
  const currentWidgets = _config.widgets;
  widgets.forEach((widget) => {
    const index = currentWidgets.findIndex((n) => n.name === widget.name);
    if (index > -1) {
      currentWidgets.splice(index, 1, merge(currentWidgets[index], widget));
    } else {
      currentWidgets.push(widget);
    }
  });
  return currentWidgets;
}

export function mergeConfig(config: IConfig) {
  const widgets = mergeWidgets(config.widgets || []);
  const setters = _config.setters.concat(config.setters || []);
  return Object.assign({}, _config, config, { widgets, setters });
}
