<commit_before>/** @jsx dom */
import clickOutside from 'click-outside';
import deepEqual from 'deep-equal';
import dom from 'magic-virtual-element';
import findIndex from 'lodash.findindex';
import uniq from 'uniq';
import Option from './option';

const propTypes = {
	class: {
		type: 'string'
	},
	forceClose: {
		type: 'boolean'
	},
	id: {
		type: 'string'
	},
	multiple: {
		type: 'boolean'
	},
	name: {
		type: 'string'
	},
	onChange: {
		type: 'function'
	},
	options: {
		type: 'array'
	},
	placeholder: {
		type: 'string'
	},
	symbol: {
		type: 'string'
	},
	showValue: {
		type: 'boolean'
	},
	toggle: {
		type: 'boolean'
	}
};

const defaultProps = {
	showValue: true,
	symbol: '\u2a2f'
};

const initialState = () => {
	return {
		active: []
	};
};

const afterMount = ({props, state}, el, setState) => {
	const {multiple, options} = props;
	const {active} = state;

	options.forEach(x => x.active && active.push(x));
	setState({active: multiple || !active.length ? active : [active[active.length - 1]]});

	clickOutside(el, () => setState({open: false}));
};

const onRemove = (val, props, state, setState) => {
	const {name, onChange} = props;
	const {active} = state;
	const arr = active.map(x => x.value);

	active.splice(findIndex(arr, x => deepEqual(x, val)), 1);

	setState({active});

	if (onChange) {
		onChange({[name]: uniq(active.map(x => x.value), (a, b) => !deepEqual(a, b))});
	}

	return;
};

const getText = (props, state, setState) => {
	const {multiple, placeholder, showValue, symbol} = props;
	const {active} = state;

	if (active.length && multiple && showValue) {
		return active.map(x => {
			return (
				<div class='Select-multiple'>
					<div class='Select-remove' onClick={() => onRemove(x.value, props, state, setState)}>
						{symbol}
					</div>
					{x.label}
				</div>
			);
		});
	}

	if (active.length && !multiple && showValue) {
		return <div class='Select-text'>{active[0].label}</div>;
	}

	return <div class='Select-placeholder'>{placeholder}</div>;
};

const onClick = (option, props, state, setState) => {
	const {forceClose, multiple, name, onChange} = props;
	const {active, open} = state;
	const arr = active.map(x => x.value);
	let ret = [option];

	if (arr.some(x => deepEqual(x, option.value))) {
		active.splice(findIndex(arr, x => deepEqual(x, option.value)), 1);

		const obj = {active};

		if (forceClose) {
			obj.open = false;
		}

		setState(obj);

		if (onChange) {
			onChange({[name]: uniq(active.map(x => x.value), (a, b) => !deepEqual(a, b))});
		}

		return;
	}

	if (multiple) {
		ret = active.filter(x => x.value).concat(ret);
	}

	setState({active: uniq(ret, (a, b) => !deepEqual(a, b)), open: multiple && !forceClose ? open : false});

	if (onChange) {
		onChange({[name]: uniq(ret.map(x => x.value), (a, b) => !deepEqual(a, b))});
	}
};

const getOptions = (props, state, setState) => {<commit_msg>Add option as return value (#3)<commit_after>/** @jsx dom */
import clickOutside from 'click-outside';
import deepEqual from 'deep-equal';
import dom from 'magic-virtual-element';
import findIndex from 'lodash.findindex';
import uniq from 'uniq';
import Option from './option';

const propTypes = {
	class: {
		type: 'string'
	},
	forceClose: {
		type: 'boolean'
	},
	id: {
		type: 'string'
	},
	multiple: {
		type: 'boolean'
	},
	name: {
		type: 'string'
	},
	onChange: {
		type: 'function'
	},
	options: {
		type: 'array'
	},
	placeholder: {
		type: 'string'
	},
	symbol: {
		type: 'string'
	},
	showValue: {
		type: 'boolean'
	},
	toggle: {
		type: 'boolean'
	}
};

const defaultProps = {
	showValue: true,
	symbol: '\u2a2f'
};

const initialState = () => {
	return {
		active: []
	};
};

const afterMount = ({props, state}, el, setState) => {
	const {multiple, options} = props;
	const {active} = state;

	options.forEach(x => x.active && active.push(x));
	setState({active: multiple || !active.length ? active : [active[active.length - 1]]});

	clickOutside(el, () => setState({open: false}));
};

const onRemove = (val, props, state, setState) => {
	const {name, onChange} = props;
	const {active} = state;
	const arr = active.map(x => x);

	active.splice(findIndex(arr, x => deepEqual(x, val)), 1);

	setState({active});

	if (onChange) {
		onChange({[name]: uniq(active.map(x => x.value), (a, b) => !deepEqual(a, b))}, val);
	}

	return;
};

const getText = (props, state, setState) => {
	const {multiple, placeholder, showValue, symbol} = props;
	const {active} = state;

	if (active.length && multiple && showValue) {
		return active.map(x => {
			return (
				<div class='Select-multiple'>
					<div class='Select-remove' onClick={() => onRemove(x, props, state, setState)}>
						{symbol}
					</div>
					{x.label}
				</div>
			);
		});
	}

	if (active.length && !multiple && showValue) {
		return <div class='Select-text'>{active[0].label}</div>;
	}

	return <div class='Select-placeholder'>{placeholder}</div>;
};

const onClick = (option, props, state, setState) => {
	const {forceClose, multiple, name, onChange} = props;
	const {active, open} = state;
	const arr = active.map(x => x.value);
	let ret = [option];

	if (arr.some(x => deepEqual(x, option.value))) {
		active.splice(findIndex(arr, x => deepEqual(x, option.value)), 1);

		const obj = {active};

		if (forceClose) {
			obj.open = false;
		}

		setState(obj);

		if (onChange) {
			onChange({[name]: uniq(active.map(x => x.value), (a, b) => !deepEqual(a, b))}, option);
		}

		return;
	}

	if (multiple) {
		ret = active.filter(x => x.value).concat(ret);
	}

	setState({active: uniq(ret, (a, b) => !deepEqual(a, b)), open: multiple && !forceClose ? open : false});

	if (onChange) {
		onChange({[name]: uniq(ret.map(x => x.value), (a, b) => !deepEqual(a, b))}, option);
	}
};

const getOptions = (props, state, setState) => {
{'fraction_non_alphanumeric': 0.2009337713989279, 'fraction_numerical': 0.0022479681826041847, 'mean_word_length': 3.8079800498753116, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 65, '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': '1488159', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2138, 'n_words': 516}