"use strict";

var isValue        = require("type/value/is")
  , clear          = require("es5-ext/array/#/clear")
  , eIndexOf       = require("es5-ext/array/#/e-index-of")
  , setPrototypeOf = require("es5-ext/object/set-prototype-of")
  , callable       = require("es5-ext/object/valid-callable")
  , d              = require("d")
  , ee             = require("event-emitter")
  , Symbol         = require("es6-symbol")
  , iterator       = require("es6-iterator/valid-iterable")
  , forOf          = require("es6-iterator/for-of")
  , Iterator       = require("./lib/iterator")
  , isNative       = require("./is-native-implemented")
  , call           = Function.prototype.call
  , defineProperty = Object.defineProperty
  , getPrototypeOf = Object.getPrototypeOf
  , SetPoly
  , getValues
  , NativeSet;

if (isNative) NativeSet = Set;

module.exports = SetPoly = function Set(/* iterable*/) {
	var iterable = arguments[0], self;
	if (!(this instanceof SetPoly)) throw new TypeError("Constructor requires 'new'");
	if (isNative && setPrototypeOf) self = setPrototypeOf(new NativeSet(), getPrototypeOf(this));
	else self = this;
	if (isValue(iterable)) iterator(iterable);
	defineProperty(self, "__setData__", d("c", []));
	if (!iterable) return self;
	forOf(
		iterable,
		function (value) {
			if (eIndexOf.call(this, value) !== -1) return;
			this.push(value);
		},
		self.__setData__
	);
	return self;
};

if (isNative) {
	if (setPrototypeOf) setPrototypeOf(SetPoly, NativeSet);
	SetPoly.prototype = Object.create(NativeSet.prototype, { constructor: d(SetPoly) });
}

ee(
	Object.defineProperties(SetPoly.prototype, {
		add: d(function (value) {
			if (this.has(value)) return this;
			this.emit("_add", this.__setData__.push(value) - 1, value);
			return this;
		}),
		clear: d(function () {
			if (!this.__setData__.length) return;
			clear.call(this.__setData__);
			this.emit("_clear");
		}),
		delete: d(function (value) {
			var index = eIndexOf.call(this.__setData__, value);
			if (index === -1) return false;
			this.__setData__.splice(index, 1);
			this.emit("_delete", index, value);
			return true;
		}),
		entries: d(function () { return new Iterator(this, "key+value"); }),
		forEach: d(function (cb /*, thisArg*/) {
			var thisArg = arguments[1], iterator, result, value;
			callable(cb);
			iterator = this.values();
			result = iterator._next();
			while (result !== undefined) {
				value = iterator._resolve(result);
				call.call(cb, thisArg, value, value, this);
				result = iterator._next();
			}
		}),
		has: d(function (value) { return eIndexOf.call(this.__setData__, value) !== -1; }),
		keys: d((getValues = function () { return this.values(); })),
		size: d.gs(function () { return this.__setData__.length; }),
		values: d(function () { return new Iterator(this); }),
		toString: d(function () { return "[object Set]"; })
	})
);
defineProperty(SetPoly.prototype, Symbol.iterator, d(getValues));
defineProperty(SetPoly.prototype, Symbol.toStringTag, d("c", "Set"));
