"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const nanoid_1 = require("nanoid");
const EventEmitter_js_1 = __importStar(require("./utilities/EventEmitter.js"));
const Errors_js_1 = require("./Errors.js");
class Locations extends EventEmitter_js_1.default {
    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 (0, Errors_js_1.ERR_NOT_ENTERED_SPACE)();
            }
            const update = {
                profileUpdate: {
                    id: null,
                    current: self.profileData,
                },
                locationUpdate: {
                    id: (0, nanoid_1.nanoid)(),
                    previous: self.location,
                    current: location,
                },
            };
            const extras = this.space.locks.getLockExtras(self.connectionId);
            yield this.presenceUpdate(update, extras);
        });
    }
    subscribe(listenerOrEvents, listener) {
        try {
            super.on(listenerOrEvents, listener);
        }
        catch (e) {
            if (e instanceof EventEmitter_js_1.InvalidArgumentError) {
                throw new EventEmitter_js_1.InvalidArgumentError('Locations.subscribe(): Invalid arguments: ' + (0, EventEmitter_js_1.inspect)([listenerOrEvents, listener]));
            }
            else {
                throw e;
            }
        }
    }
    unsubscribe(listenerOrEvents, listener) {
        try {
            super.off(listenerOrEvents, listener);
        }
        catch (e) {
            if (e instanceof EventEmitter_js_1.InvalidArgumentError) {
                throw new EventEmitter_js_1.InvalidArgumentError('Locations.unsubscribe(): Invalid arguments: ' + (0, EventEmitter_js_1.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;
            }, {});
        });
    }
}
exports.default = Locations;
//# sourceMappingURL=Locations.js.map