<commit_before>import * as React from "react";
import * as FileSaver from "filesaver.js";
import {MenuBarItemProps} from "../react-ui/MenuBar";
import {MenuItemProps} from "../react-ui/Menu";
import DataSourceManager from "../ui/DataSourceManager";
import IDataSource = weavejs.api.data.IDataSource;
import FileMenu from "./FileMenu";
import FileInput from "../react-ui/FileInput";
import ToolsMenu from "./ToolsMenu";

export default class DataMenu implements MenuBarItemProps
{
	constructor(weave:Weave, createObject:(type:new(..._:any[])=>any)=>void)
	{
		this.weave = weave;
		this.fileMenu = new FileMenu(weave);
		this.toolsMenu = new ToolsMenu(weave, createObject);
		this.createObject = createObject;
	}

	label:string = "Data";
	weave:Weave;
	toolsMenu:ToolsMenu;//menu to be passed for creating visualizations from tha datasource manager
	fileMenu:FileMenu; // temp solution
	createObject:(type:new(..._:any[])=>any)=>void;
	
	get menu():MenuItemProps[]
	{
		return [].concat(
			{
				shown: Weave.beta,
				label: <FileInput onChange={(()=>alert('Not implemented yet')) || this.fileMenu.openFile} accept={this.fileMenu.getSupportedFileTypes(true).join(',')}>{Weave.lang("Import data file(s)... (not implemented yet)")}</FileInput>
			},
			{
				enabled: this.getColumnsToExport().length > 0,
				label: Weave.lang("Export CSV"),
				click: this.exportCSV
			},
			{},
			{
				label: Weave.lang('Manage or browse data'),
				click: () => DataSourceManager.openInstance(this)
			},
			{},
			this.getDataSourceItems()
		);
	}

	getDataSourceItems()
	{
		var registry = weavejs.WeaveAPI.ClassRegistry;
		var impls = registry.getImplementations(IDataSource);
		
		// filter out those data sources without editors
		impls = impls.filter(impl => DataSourceManager.editorRegistry.has(impl));
		
		return impls.map(impl => {
			var label = Weave.lang('+ {0}', registry.getDisplayName(impl));
			return {
				get shown() {
					return Weave.beta || !DataMenu.isBeta(impl);
				},
				get label() {
					if (DataMenu.isBeta(impl))
						return label + " (beta)";
					return label;
				},
				click: this.createObject.bind(this, impl)
			}
		});
	}

	static isBeta(impl:new()=>IDataSource):boolean
	{
		return impl == weavejs.data.source.CensusDataSource;
	}
	
	getColumnsToExport=()=><commit_msg>GroupedDataTransform beta<commit_after>import * as React from "react";
import * as FileSaver from "filesaver.js";
import {MenuBarItemProps} from "../react-ui/MenuBar";
import {MenuItemProps} from "../react-ui/Menu";
import DataSourceManager from "../ui/DataSourceManager";
import IDataSource = weavejs.api.data.IDataSource;
import FileMenu from "./FileMenu";
import FileInput from "../react-ui/FileInput";
import ToolsMenu from "./ToolsMenu";

export default class DataMenu implements MenuBarItemProps
{
	constructor(weave:Weave, createObject:(type:new(..._:any[])=>any)=>void)
	{
		this.weave = weave;
		this.fileMenu = new FileMenu(weave);
		this.toolsMenu = new ToolsMenu(weave, createObject);
		this.createObject = createObject;
	}

	label:string = "Data";
	weave:Weave;
	toolsMenu:ToolsMenu;//menu to be passed for creating visualizations from tha datasource manager
	fileMenu:FileMenu; // temp solution
	createObject:(type:new(..._:any[])=>any)=>void;
	
	get menu():MenuItemProps[]
	{
		return [].concat(
			{
				shown: Weave.beta,
				label: <FileInput onChange={(()=>alert('Not implemented yet')) || this.fileMenu.openFile} accept={this.fileMenu.getSupportedFileTypes(true).join(',')}>{Weave.lang("Import data file(s)... (not implemented yet)")}</FileInput>
			},
			{
				enabled: this.getColumnsToExport().length > 0,
				label: Weave.lang("Export CSV"),
				click: this.exportCSV
			},
			{},
			{
				label: Weave.lang('Manage or browse data'),
				click: () => DataSourceManager.openInstance(this)
			},
			{},
			this.getDataSourceItems()
		);
	}

	getDataSourceItems()
	{
		var registry = weavejs.WeaveAPI.ClassRegistry;
		var impls = registry.getImplementations(IDataSource);
		
		// filter out those data sources without editors
		impls = impls.filter(impl => DataSourceManager.editorRegistry.has(impl));
		
		return impls.map(impl => {
			var label = Weave.lang('+ {0}', registry.getDisplayName(impl));
			return {
				get shown() {
					return Weave.beta || !DataMenu.isBeta(impl);
				},
				get label() {
					if (DataMenu.isBeta(impl))
						return label + " (beta)";
					return label;
				},
				click: this.createObject.bind(this, impl)
			}
		});
	}

	static isBeta(impl:new()=>IDataSource):boolean
	{
		return impl == weavejs.data.source.CensusDataSource
			|| impl == weavejs.data.source.GroupedDataTransform;
	}
	
	getColumnsToExport=()=>
{'fraction_non_alphanumeric': 0.1539944313557507, 'fraction_numerical': 0.0008567144998929107, 'mean_word_length': 4.154525386313466, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4194001', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1497, 'n_words': 314}