import React from 'react';
import { Label } from 'semantic-ui-react'
import Cookies from 'universal-cookie';
import * as antd from 'antd'
import * as Shared from 'placeholder-shared'
import * as _ from 'lodash'
import { shallowEqual, useSelector } from 'react-redux'
import { stringType } from 'aws-sdk/clients/iam';
import sqlFormatter from "sql-formatter";

export var SC = require ('./SComponents')
export var classNames = require('classnames')
export interface IPoint {
	       x:number, y:number;
	}

interface MyProps{
	method: string
}

export const max= (a, initialValue)=> {
	return a.reduce((m,x)=> parseInt(m)>parseInt(x) ? parseInt(m):parseInt(x), initialValue);
}

export const editorOptions = {lineNumbersMinChars:3,
	lineDecorationsWidth:0,
	showFoldingControls:'mouseover',
	glyphMargin:false,'lineNumbers':true, fontSize:11.5, minimap:{enabled:false}, codeLens:false, wordWrap:'on', scrollBeyondLastLine:false, 'quickSuggestions':false, 'contextmenu':false, 'suggest':false}

export const getTopLeftFromMiddlePointsAndSize = (x:number, y:number, width:number, height:number) =>{
	return [
		x - (width/2), y - (height / 2)
	]
}

export const TreeLayoutOptionsMain = {
	'rankdir': 'LR',
	//'align':'DL',
	'ranker': 'network-simplex',
	'nodesep': 3,
	'ranksep': 20,
	'edgesep':10,
	'marginx': 5,
	'marginy': 75
}

export const TreeLayoutOptionsStep = {
	'rankdir': 'LR',
	'align':'DL',
	'ranker': 'longest-path',
	'nodesep': 50,
	'ranksep': 25,
	'marginx': 2,
	'marginy':2 
}

type DivWithClassNameProps= {
	id?:string;
	className?:string;
	draggable?:boolean
	children?:React.ReactNode;
	onKeyUp?:(e:React.KeyboardEvent) =>any;
	onContextMenu?:(e:React.MouseEvent) => any;
	onDrag?:any;
	onDragStart?:(e:React.DragEvent) =>any;
	onDragEnd?:(e:React.DragEvent) =>any;
	onDragOver?:(e:React.DragEvent) =>any;
	onDragLeave?:(e:React.DragEvent) =>any;
	onDrop?:(e:React.DragEvent) => any;
	onClick?:(e:React.MouseEvent) =>any;
	onMouseDown?:(e:React.MouseEvent) =>any;
	onMouseUp?:(e:React.MouseEvent) =>any;
	onMouseEnter?:(e:React.MouseEvent) =>any;
	onMouseLeave?:(e:React.MouseEvent) =>any;
	style?:any;
}
export const DivWithClassName = (props:DivWithClassNameProps) => {
	const {style,id,className, onKeyUp,onClick, onMouseUp,onMouseDown,onMouseEnter,onMouseLeave,children, draggable, onContextMenu, onDragStart, onDragOver, onDragLeave, onDragEnd, onDrop, onDrag} = props
	return <div onDrag={onDrag} onKeyUp={onKeyUp} onMouseUp={onMouseUp} onMouseDown={onMouseDown} onDrop={onDrop} style={style} id={id} onDragLeave={onDragLeave} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} onContextMenu={onContextMenu} onClick={onClick} onDragEnd={onDragEnd} onDragStart={onDragStart} onDragOver={onDragOver} draggable={draggable} className={classNames(className)}>
		{children}
		</div>
}
type ElementWithClassNameProps= {
	className?:string;
	children?:React.ReactNode;
	elementType:string;
}
export const ElementWithClassName = (props:ElementWithClassNameProps) => {
	const {className, children, elementType} = props
	return React.createElement(elementType, 
		{className}, children)
}


export const isDev = () => {
	if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
		return true;
	}
	return false;

}

export const layout = {
	labelCol: { span: 3 },
	wrapperCol: { span: 18 },
};
export const tailLayout = {
	wrapperCol: { offset: 3, span: 18 },
};

export const FormatSQL = (sql:string) => {
	const formattedSQL = sqlFormatter.format(sql)
	


	//the library we are using for formatting code turns ${ref} into $ { ref }, so after formatting, go back and fix these by removing whitespaces
	const getInvalidRefRegex = () => {
		return new RegExp(/\$ \{ ([a-zA-Z0-9\s\_\-]*) \}/g)
	}
	
	let processedSQL = formattedSQL.slice(0)
	let regex = getInvalidRefRegex()

	let array1;
	while ((array1 = regex.exec(processedSQL)) !== null) {
		const tableRefName = array1[1].trim()
		const getProcessedTableName = `\$\{${tableRefName}\}`
		processedSQL = processedSQL.replace(array1[0], getProcessedTableName);
	}

	console.log("processed SQL", processedSQL)
	return processedSQL

}