'use strict';
var $ = require('../internals/export');
var $toString = require('../internals/to-string');
var anObject = require('../internals/an-object');
var isNullOrUndefined = require('../internals/is-null-or-undefined');
var iterate = require('../internals/iterate');
var iteratorClose = require('../internals/iterator-close');
var uncurryThis = require('../internals/function-uncurry-this');
var IS_PURE = require('../internals/is-pure');

var $join = uncurryThis([].join);
var push = uncurryThis([].push);

// `Iterator.prototype.join` method
// https://github.com/tc39/proposal-iterator-join
$({ target: 'Iterator', proto: true, real: true, forced: IS_PURE }, {
  join: function join(separator) {
    anObject(this);
    var sep;
    try {
      sep = separator === undefined ? ',' : $toString(separator);
    } catch (error) {
      iteratorClose(this, 'throw', error);
    }
    var result = [];
    iterate(this, function (value) {
      push(result, isNullOrUndefined(value) ? '' : $toString(value));
    }, { IS_ITERATOR: true });
    return $join(result, sep);
  }
});
