10fbfc30aSopenharmony_ci/*
20fbfc30aSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
30fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
50fbfc30aSopenharmony_ci * You may obtain a copy of the License at
60fbfc30aSopenharmony_ci *
70fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80fbfc30aSopenharmony_ci *
90fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
130fbfc30aSopenharmony_ci * limitations under the License.
140fbfc30aSopenharmony_ci */
150fbfc30aSopenharmony_ciconst calendarManager = requireInternal('calendarManager');
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ciclass JsCalendarManager {
180fbfc30aSopenharmony_ci  nativeCalendarManager;
190fbfc30aSopenharmony_ci
200fbfc30aSopenharmony_ci  constructor(calendarManager) {
210fbfc30aSopenharmony_ci    this.nativeCalendarManager = calendarManager;
220fbfc30aSopenharmony_ci  }
230fbfc30aSopenharmony_ci
240fbfc30aSopenharmony_ci  async createCalendar(calendarAccount, callback) {
250fbfc30aSopenharmony_ci    if (callback) {
260fbfc30aSopenharmony_ci      this.nativeCalendarManager.createCalendar(calendarAccount, callback);
270fbfc30aSopenharmony_ci    } else {
280fbfc30aSopenharmony_ci      return await this.nativeCalendarManager.createCalendar(calendarAccount);
290fbfc30aSopenharmony_ci    }
300fbfc30aSopenharmony_ci  }
310fbfc30aSopenharmony_ci
320fbfc30aSopenharmony_ci  async deleteCalendar(calendar, callback) {
330fbfc30aSopenharmony_ci    if (callback) {
340fbfc30aSopenharmony_ci      this.nativeCalendarManager.deleteCalendar(calendar, callback);
350fbfc30aSopenharmony_ci    } else {
360fbfc30aSopenharmony_ci      return await this.nativeCalendarManager.deleteCalendar(calendar);
370fbfc30aSopenharmony_ci    }
380fbfc30aSopenharmony_ci  }
390fbfc30aSopenharmony_ci
400fbfc30aSopenharmony_ci  async getCalendar(calendarAccount, callback) {
410fbfc30aSopenharmony_ci    let calendar;
420fbfc30aSopenharmony_ci    if (calendarAccount) {
430fbfc30aSopenharmony_ci      if (callback) {
440fbfc30aSopenharmony_ci        this.nativeCalendarManager.getCalendar(calendarAccount, callback);
450fbfc30aSopenharmony_ci      } else {
460fbfc30aSopenharmony_ci        return await this.nativeCalendarManager.getCalendar(calendarAccount);
470fbfc30aSopenharmony_ci      }
480fbfc30aSopenharmony_ci    } else {
490fbfc30aSopenharmony_ci      if (callback) {
500fbfc30aSopenharmony_ci        this.nativeCalendarManager.getCalendar(callback);
510fbfc30aSopenharmony_ci      } else {
520fbfc30aSopenharmony_ci        return await this.nativeCalendarManager.getCalendar();
530fbfc30aSopenharmony_ci      }
540fbfc30aSopenharmony_ci    }
550fbfc30aSopenharmony_ci  }
560fbfc30aSopenharmony_ci
570fbfc30aSopenharmony_ci  async getAllCalendars(callback) {
580fbfc30aSopenharmony_ci    if (callback) {
590fbfc30aSopenharmony_ci      this.nativeCalendarManager.getAllCalendars(callback);
600fbfc30aSopenharmony_ci    } else {
610fbfc30aSopenharmony_ci      return await this.nativeCalendarManager.getAllCalendars();
620fbfc30aSopenharmony_ci    }
630fbfc30aSopenharmony_ci  }
640fbfc30aSopenharmony_ci
650fbfc30aSopenharmony_ci  async editEvent(event) {
660fbfc30aSopenharmony_ci    console.log('JsCalendarManager editEvent called');
670fbfc30aSopenharmony_ci    let context = getContext(this);
680fbfc30aSopenharmony_ci    let eventStr = JSON.stringify(event);
690fbfc30aSopenharmony_ci    let callerPkg = context.applicationInfo?.name;
700fbfc30aSopenharmony_ci    return await this.nativeCalendarManager.editEvent(context, eventStr, callerPkg);
710fbfc30aSopenharmony_ci  }
720fbfc30aSopenharmony_ci}
730fbfc30aSopenharmony_ci
740fbfc30aSopenharmony_cilet mJsCalendarManager;
750fbfc30aSopenharmony_ci
760fbfc30aSopenharmony_cifunction getCalendarManager(context) {
770fbfc30aSopenharmony_ci  let nativeCalendarManager = calendarManager.getCalendarManager(context);
780fbfc30aSopenharmony_ci  if (mJsCalendarManager === null || mJsCalendarManager === undefined ||
790fbfc30aSopenharmony_ci    nativeCalendarManager !== mJsCalendarManager.nativeCalendarManager) {
800fbfc30aSopenharmony_ci    // 如果native层拿到的manager不一样了,就也重新创建一个JSCalendarManager包装对象
810fbfc30aSopenharmony_ci    mJsCalendarManager = new JsCalendarManager(nativeCalendarManager);
820fbfc30aSopenharmony_ci  }
830fbfc30aSopenharmony_ci  return mJsCalendarManager;
840fbfc30aSopenharmony_ci}
850fbfc30aSopenharmony_ci
860fbfc30aSopenharmony_ciexport default {
870fbfc30aSopenharmony_ci  getCalendarManager: getCalendarManager,
880fbfc30aSopenharmony_ci  CalendarManager: JsCalendarManager,
890fbfc30aSopenharmony_ci  Calendar: calendarManager.Calendar,
900fbfc30aSopenharmony_ci  CalendarAccount: calendarManager.CalendarAccount,
910fbfc30aSopenharmony_ci  CalendarConfig: calendarManager.CalendarConfig,
920fbfc30aSopenharmony_ci  Event: calendarManager.Event,
930fbfc30aSopenharmony_ci  CalendarType: calendarManager.CalendarType,
940fbfc30aSopenharmony_ci  Location: calendarManager.Location,
950fbfc30aSopenharmony_ci  EventFilter: calendarManager.EventFilter,
960fbfc30aSopenharmony_ci  EventType: calendarManager.EventType,
970fbfc30aSopenharmony_ci  RecurrenceRule: calendarManager.RecurrenceRule,
980fbfc30aSopenharmony_ci  RecurrenceFrequency: calendarManager.RecurrenceFrequency,
990fbfc30aSopenharmony_ci  Attendee: calendarManager.Attendee,
1000fbfc30aSopenharmony_ci  EventService: calendarManager.EventService,
1010fbfc30aSopenharmony_ci  ServiceType: calendarManager.ServiceType,
1020fbfc30aSopenharmony_ci  AttendeeRole: calendarManager.AttendeeRole,
1030fbfc30aSopenharmony_ci};