import { isArray } from '../../utility/kit';
import { Node } from '../../node/node';

export class SelectionState {
	constructor(model, service) {
		this.model = model;
		this.service = service;
	}

	select(item, state = true, key) {
		key = key || this.keyFactory();
		if (isArray(item)) {
			item.forEach(item => this.select(item, state, key));
			return;
		}

		if (item instanceof Node) {
			const { rows } = this.model.data();
			if (rows.length) {
				item.rows.forEach(index => this.select(rows[index], state, key));
				return;
			}
		}

		this.selectCore(item, state, key);
	}

	canSelect(item) {
		return this.canSelectCore(item);
	}

	toggle(item) {
		const state = this.state(item);
		return this.select(item, state === null || !state);
	}

	state(item, key) {
		key = key || this.keyFactory();
		if (isArray(item)) {
			const all = item.every(item => this.state(item, key));
			return all ? true : item.some(item => this.state(item, key)) ? null : false;
		}

		if (item instanceof Node) {
			const { rows } = this.model.data();
			if (rows.length) {
				const all = item.rows.length && item.rows.every(index => this.state(rows[index], key));
				return all ? true : item.rows.some(index => this.state(rows[index], key)) ? null : false;
			}
		}

		return this.stateCore(item, key);
	}

	stateAll(items) {
		const entries = this.entries();
		if (items.length === entries.length) {
			return true;
		}

		return entries.length > 0 ? null : false;
	}

	keyFactory() {
		return this.service.hashFactory();
	}

	clear() {
		this.clearCore();
	}

	entries() {
		return [];
	}

	selectCore() {
	}

	clearCore() {
	}

	stateCore() {
		return false;
	}

	canSelectCore() {
		return true;
	}
}
@@ -1318,144 +1318,328 @@
 %7B%0A%09%09
-const entries = this.entries
+if (!items.length) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%0A%09%09const key = this.keyFactory
 ();%0A
+%0A
 %09%09
-if (items.length === entries.length) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%0A%09%09return entries.length %3E 0 ? null : false
+const notSelected = items.findIndex(item =%3E this.state(item, key) === false);%0A%09%09if (notSelected %3C 0) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%0A%09%09return notSelected === 0%0A%09%09%09? items.every(item =%3E this.state(item, key) === false)%0A%09%09%09%09? false%0A%09%09%09%09: null%0A%09%09%09: null
 ;%0A%09%7D

fix select all state