18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ci
168779efd5Sopenharmony_ciimport Contact from '../entity/Contact';
178779efd5Sopenharmony_ciimport RawContactDelta from './RawContactDelta';
188779efd5Sopenharmony_ciimport { RawContacts } from '../contract/RawContacts';
198779efd5Sopenharmony_ciimport DAOperation from './DAOperation';
208779efd5Sopenharmony_ci
218779efd5Sopenharmony_ciexport default class ContactDelta {
228779efd5Sopenharmony_ci  readonly rawContactDeltas: RawContactDelta[];
238779efd5Sopenharmony_ci  constructor() {
248779efd5Sopenharmony_ci    this.rawContactDeltas = [];
258779efd5Sopenharmony_ci  }
268779efd5Sopenharmony_ci
278779efd5Sopenharmony_ci  static fromContact(contact: Contact) {
288779efd5Sopenharmony_ci    let contactDelta = new ContactDelta();
298779efd5Sopenharmony_ci    for (let rawContact of contact.rowContacts) {
308779efd5Sopenharmony_ci      let rawContactDelta = RawContactDelta.fromRawContact(rawContact);
318779efd5Sopenharmony_ci      contactDelta.rawContactDeltas.push(rawContactDelta);
328779efd5Sopenharmony_ci    }
338779efd5Sopenharmony_ci    return contactDelta;
348779efd5Sopenharmony_ci  }
358779efd5Sopenharmony_ci
368779efd5Sopenharmony_ci  buildDiff() {
378779efd5Sopenharmony_ci    let diff = [];
388779efd5Sopenharmony_ci    for (let i = 0; i < this.rawContactDeltas.length; i++) {
398779efd5Sopenharmony_ci      let rawContactDelta = this.rawContactDeltas[i];
408779efd5Sopenharmony_ci      rawContactDelta.buildDiff(diff);
418779efd5Sopenharmony_ci    }
428779efd5Sopenharmony_ci    return diff;
438779efd5Sopenharmony_ci  }
448779efd5Sopenharmony_ci
458779efd5Sopenharmony_ci  private findRawContactId() {
468779efd5Sopenharmony_ci    for (let i = 0; i < this.rawContactDeltas.length; i++) {
478779efd5Sopenharmony_ci      let rawContactId = this.rawContactDeltas[i].values.getValue(RawContacts.ID);
488779efd5Sopenharmony_ci      if (rawContactId != undefined && rawContactId >= 0) {
498779efd5Sopenharmony_ci        return rawContactId;
508779efd5Sopenharmony_ci      }
518779efd5Sopenharmony_ci    }
528779efd5Sopenharmony_ci    return -1;
538779efd5Sopenharmony_ci  }
548779efd5Sopenharmony_ci}