1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import AuthorizedAccount from './AuthorizedAccount'; 17import { HiLog } from '../../common/HiLog'; 18 19const TAG: string = 'AccAssociation'; 20 21export default class AccountAssociation { 22 private accountId: string; 23 private authorizedAccounts: AuthorizedAccount[]; 24 25 constructor(accountId: string, authorizedAccounts: AuthorizedAccount[]) { 26 this.accountId = accountId; 27 this.authorizedAccounts = authorizedAccounts; 28 } 29 30 public getAccountId(): string { 31 return this.accountId; 32 } 33 34 public setAccountId(accountId: string): void { 35 this.accountId = accountId; 36 } 37 38 public getAuthorizedAccounts(): AuthorizedAccount[] { 39 return this.authorizedAccounts; 40 } 41 42 public setAuthorizedAccounts(authorizedAccounts: AuthorizedAccount[]): void { 43 this.authorizedAccounts = authorizedAccounts; 44 } 45 46 public removeEarliestAuthorizedAccount(): AuthorizedAccount | null { 47 if (!this.authorizedAccounts || this.authorizedAccounts.length === 0) { 48 HiLog.error(TAG, 'The authorized account array is invalid.'); 49 return null; 50 } 51 let earliestAccount: AuthorizedAccount = this.authorizedAccounts.reduce((min, current) => { 52 return min.getTimestamp() < current.getTimestamp() ? min : current; 53 }, this.authorizedAccounts[0]); 54 55 this.authorizedAccounts.splice(this.authorizedAccounts.indexOf(earliestAccount), 1); 56 return earliestAccount; 57 } 58}