/* eslint-disable @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars,unicorn/no-null,jsdoc/require-jsdoc,@typescript-eslint/no-explicit-any,@typescript-eslint/ban-types */
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Drifty, co.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
// This is based on the Ionic Storage 4.0.0 implementation until we can
// upgrade to Angular 14+ and use the new update.
// https://github.com/ionic-team/ionic-storage/blob/main/angular/src/index.ts
import {isPlatformServer} from '@angular/common';
import type {ModuleWithProviders} from '@angular/core';
import {InjectionToken, NgModule, PLATFORM_ID} from '@angular/core';
import {Storage, StorageConfig} from '@ionic/storage';

const StorageConfigToken = new InjectionToken<any>('STORAGE_CONFIG_TOKEN');

export {StorageConfigToken};

class NoopStorage extends Storage {
  constructor() {
    super();
  }

  async create() {
    return this;
  }

  async defineDriver() {}

  get driver(): string | null {
    return 'noop';
  }

  // @ts-expect-error unused
  async get(key: string) {
    return null;
  }

  // @ts-expect-error unused
  async set(key: string, value: any) {}

  // @ts-expect-error unused
  async remove(key: string): Promise<any> {}

  async clear(): Promise<void> {}

  async length(): Promise<number> {
    return 0;
  }

  async keys() {
    return [];
  }

  // @ts-expect-error unused
  async forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any): Promise<void> {}

  // @ts-expect-error unused
  setEncryptionKey(key: string) {}
}

export function provideStorage(platformId: any, storageConfig: StorageConfig): Storage {
  if (isPlatformServer(platformId)) {
    // When running in a server context return the NoopStorage
    return new NoopStorage();
  }

  return new Storage(storageConfig);
}

@NgModule()
export class IonicStorageModule {
  // @ts-expect-error unused
  static forRoot(storageConfig: StorageConfig = null): ModuleWithProviders<IonicStorageModule> {
    return {
      ngModule: IonicStorageModule,
      providers: [
        {provide: StorageConfigToken, useValue: storageConfig},
        {
          provide: Storage,
          useFactory: provideStorage,
          deps: [PLATFORM_ID, StorageConfigToken],
        },
      ],
    };
  }
}
