var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { ERR_SPACE_NAME_MISSING } from './Errors.js';
import Space from './Space.js';
class Spaces {
    constructor(client) {
        this.spaces = {};
        this.version = '0.1.1';
        this.client = client;
        this.connection = client.connection;
        this.addAgent(this.client['options']);
        this.client.time();
    }
    addAgent(options) {
        var _a;
        const agent = { 'ably-spaces': this.version, 'space-custom-client': true };
        options.agents = Object.assign(Object.assign({}, ((_a = options.agents) !== null && _a !== void 0 ? _a : options.agents)), agent);
    }
    get(name, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (typeof name !== 'string' || name.length === 0) {
                throw ERR_SPACE_NAME_MISSING();
            }
            if (this.connection.state !== 'connected') {
                yield this.connection.once('connected');
            }
            if (this.spaces[name])
                return this.spaces[name];
            const space = new Space(name, this.client, options);
            this.spaces[name] = space;
            return space;
        });
    }
}
export default Spaces;
//# sourceMappingURL=Spaces.js.map