"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CallMembership = void 0;
var _utils = require("../utils");
/*
Copyright 2023 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Represents an entry in the memberships section of an m.call.member event as it is on the wire

class CallMembership {
  static equal(a, b) {
    return (0, _utils.deepCompare)(a.data, b.data);
  }
  constructor(parentEvent, data) {
    this.parentEvent = parentEvent;
    this.data = data;
    if (typeof data.expires !== "number") throw new Error("Malformed membership: expires must be numeric");
    if (typeof data.device_id !== "string") throw new Error("Malformed membership event: device_id must be string");
    if (typeof data.call_id !== "string") throw new Error("Malformed membership event: call_id must be string");
    if (typeof data.scope !== "string") throw new Error("Malformed membership event: scope must be string");
    if (!parentEvent.getSender()) throw new Error("Invalid parent event: sender is null");
  }
  get sender() {
    return this.parentEvent.getSender();
  }
  get callId() {
    return this.data.call_id;
  }
  get deviceId() {
    return this.data.device_id;
  }
  get application() {
    return this.data.application;
  }
  get scope() {
    return this.data.scope;
  }
  get membershipID() {
    return this.data.membershipID;
  }
  createdTs() {
    var _this$data$created_ts;
    return (_this$data$created_ts = this.data.created_ts) !== null && _this$data$created_ts !== void 0 ? _this$data$created_ts : this.parentEvent.getTs();
  }
  getAbsoluteExpiry() {
    return this.createdTs() + this.data.expires;
  }

  // gets the expiry time of the event, converted into the device's local time
  getLocalExpiry() {
    const relativeCreationTime = this.parentEvent.getTs() - this.createdTs();
    const localCreationTs = this.parentEvent.localTimestamp - relativeCreationTime;
    return localCreationTs + this.data.expires;
  }
  getMsUntilExpiry() {
    return this.getLocalExpiry() - Date.now();
  }
  isExpired() {
    return this.getAbsoluteExpiry() < this.parentEvent.getTs() + this.parentEvent.getLocalAge();
  }
  getActiveFoci() {
    var _this$data$foci_activ;
    return (_this$data$foci_activ = this.data.foci_active) !== null && _this$data$foci_activ !== void 0 ? _this$data$foci_activ : [];
  }
}
exports.CallMembership = CallMembership;
//# sourceMappingURL=CallMembership.js.map