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 EventEmitter, { InvalidArgumentError, inspect, } from './utilities/EventEmitter.js';
import { ERR_NOT_ENTERED_SPACE } from './Errors.js';
import SpaceUpdate from './SpaceUpdate.js';
export default class Locations extends EventEmitter {
    constructor(space, presenceUpdate) {
        super();
        this.space = space;
        this.presenceUpdate = presenceUpdate;
        this.lastLocationUpdate = {};
    }
    processPresenceMessage(message) {
        return __awaiter(this, void 0, void 0, function* () {
            // Only an update action is currently a valid location update.
            if (message.action !== 'update')
                return;
            // Emit updates only if they are different than the last held update.
            if (!message.data.locationUpdate.id ||
                this.lastLocationUpdate[message.connectionId] === message.data.locationUpdate.id) {
                return;
            }
            const update = message.data.locationUpdate;
            const { previous } = update;
            const member = yield this.space.members.getByConnectionId(message.connectionId);
            if (member) {
                this.emit('update', {
                    member,
                    currentLocation: member.location,
                    previousLocation: previous,
                });
                this.lastLocationUpdate[message.connectionId] = message.data.locationUpdate.id;
            }
        });
    }
    set(location) {
        return __awaiter(this, void 0, void 0, function* () {
            const self = yield this.space.members.getSelf();
            if (!self) {
                throw ERR_NOT_ENTERED_SPACE();
            }
            const update = new SpaceUpdate({ self, extras: this.space.locks.getLockExtras(self.connectionId) });
            yield this.presenceUpdate(update.updateLocation(location));
        });
    }
    subscribe(listenerOrEvents, listener) {
        try {
            super.on(listenerOrEvents, listener);
        }
        catch (e) {
            if (e instanceof InvalidArgumentError) {
                throw new InvalidArgumentError('Locations.subscribe(): Invalid arguments: ' + inspect([listenerOrEvents, listener]));
            }
            else {
                throw e;
            }
        }
    }
    unsubscribe(listenerOrEvents, listener) {
        try {
            super.off(listenerOrEvents, listener);
        }
        catch (e) {
            if (e instanceof InvalidArgumentError) {
                throw new InvalidArgumentError('Locations.unsubscribe(): Invalid arguments: ' + inspect([listenerOrEvents, listener]));
            }
            else {
                throw e;
            }
        }
    }
    getSelf() {
        return __awaiter(this, void 0, void 0, function* () {
            const self = yield this.space.members.getSelf();
            return self ? self.location : null;
        });
    }
    getOthers() {
        return __awaiter(this, void 0, void 0, function* () {
            const members = yield this.space.members.getOthers();
            return members.reduce((acc, member) => {
                acc[member.connectionId] = member.location;
                return acc;
            }, {});
        });
    }
    getAll() {
        return __awaiter(this, void 0, void 0, function* () {
            const members = yield this.space.members.getAll();
            return members.reduce((acc, member) => {
                acc[member.connectionId] = member.location;
                return acc;
            }, {});
        });
    }
}
//# sourceMappingURL=Locations.js.map