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 = { ...((_a = options.agents) !== null && _a !== void 0 ? _a : options.agents), ...agent };
    }
    async get(name, options) {
        if (typeof name !== 'string' || name.length === 0) {
            throw ERR_SPACE_NAME_MISSING();
        }
        if (this.connection.state !== 'connected') {
            await 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;
