import { deprecate } from '@ember/debug';
import JSONAPICache from '@ember-data/json-api';
import { LegacyNetworkHandler, adapterFor, serializerFor, pushPayload, normalize, serializeRecord, cleanup } from '@ember-data/legacy-compat';
import { buildSchema, instantiateRecord, teardownRecord, modelFor } from '@ember-data/model';
import RequestManager from '@ember-data/request';
import Fetch from '@ember-data/request/fetch';
import BaseStore, { CacheHandler } from '@ember-data/store';
import { macroCondition, getGlobalConfig } from '@embroider/macros';

function hasRequestManager(store) {
  return 'requestManager' in store;
}
class Store extends BaseStore {
  constructor(args) {
    super(args);
    if (!hasRequestManager(this)) {
      this.requestManager = new RequestManager();
      this.requestManager.use(macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS) ? [LegacyNetworkHandler, Fetch] : [Fetch]);
    }
    this.requestManager.useCache(CacheHandler);
  }
  createSchemaService() {
    return buildSchema(this);
  }
  createCache(storeWrapper) {
    return new JSONAPICache(storeWrapper);
  }
  instantiateRecord(key, createRecordArgs) {
    return instantiateRecord.call(this, key, createRecordArgs);
  }
  teardownRecord(record) {
    return teardownRecord.call(this, record);
  }
  modelFor(type) {
    return modelFor.call(this, type) || super.modelFor(type);
  }
  adapterFor(modelName, _allowMissing) {
    if (macroCondition(!getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS)) {
      macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
        {
          throw new Error(`You cannot use store.adapterFor when ENABLE_LEGACY_REQUEST_METHODS is false without explicitly registering the adapterFor hook from @ember-data/legacy-compat or @warp-drive/legacy.`);
        }
      })() : {};
    } else {
      deprecate(`store.adapterFor is deprecated, please use store.request to perform requests and builders/handlers/utils to produce and process them, or explicitly register the adapterFor hook and LegacyNetworkHandler from @ember-data/legacy-compat or @warp-drive/legacy.`, false, {
        id: 'warp-drive:deprecate-legacy-request-methods',
        until: '6.0',
        for: '@warp-drive/core',
        url: 'https://docs.warp-drive.io/api/@warp-drive/core/build-config/deprecations/variables/ENABLE_LEGACY_REQUEST_METHODS',
        since: {
          enabled: '5.7',
          available: '5.7'
        }
      });

      // @ts-expect-error
      return adapterFor.call(this, modelName, _allowMissing);
    }
  }
  serializerFor = (...args) => {
    if (macroCondition(!getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS)) {
      macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
        {
          throw new Error(`You cannot use store.serializerFor when ENABLE_LEGACY_REQUEST_METHODS is false without explicitly registering the serializerFor hook from @ember-data/legacy-compat or @warp-drive/legacy.`);
        }
      })() : {};
    } else {
      deprecate(`store.serializerFor is deprecated, please use store.request to perform requests and builders/handlers/utils to produce and process them, or explicitly register the serializerFor hook and LegacyNetworkHandler from @ember-data/legacy-compat or @warp-drive/legacy.`, false, {
        id: 'warp-drive:deprecate-legacy-request-methods',
        until: '6.0',
        for: '@warp-drive/core',
        url: 'https://docs.warp-drive.io/api/@warp-drive/core/build-config/deprecations/variables/ENABLE_LEGACY_REQUEST_METHODS',
        since: {
          enabled: '5.7',
          available: '5.7'
        }
      });
      return serializerFor.call(this, ...args);
    }
  };
  pushPayload = (...args) => {
    if (macroCondition(!getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS)) {
      macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
        {
          throw new Error(`You cannot use store.pushPayload when ENABLE_LEGACY_REQUEST_METHODS is false without explicitly registering the pushPayload hook from @ember-data/legacy-compat or @warp-drive/legacy.`);
        }
      })() : {};
    } else {
      deprecate(`store.pushPayload is deprecated, please use store.request to perform requests and builders/handlers/utils to produce and process them, or explicitly register the pushPayload and serializerFor hooks from @ember-data/legacy-compat or @warp-drive/legacy.`, false, {
        id: 'warp-drive:deprecate-legacy-request-methods',
        until: '6.0',
        for: '@warp-drive/core',
        url: 'https://docs.warp-drive.io/api/@warp-drive/core/build-config/deprecations/variables/ENABLE_LEGACY_REQUEST_METHODS',
        since: {
          enabled: '5.7',
          available: '5.7'
        }
      });
      return pushPayload.call(this, ...args);
    }
  };
  normalize = (...args) => {
    if (macroCondition(!getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS)) {
      macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
        {
          throw new Error(`You cannot use store.normalize when ENABLE_LEGACY_REQUEST_METHODS is false without explicitly registering the normalize hook from @ember-data/legacy-compat or @warp-drive/legacy.`);
        }
      })() : {};
    } else {
      deprecate(`store.normalize is deprecated, please use store.request to perform requests and builders/handlers/utils to produce and process them, or explicitly register the normalize and serializerFor hooks from @ember-data/legacy-compat or @warp-drive/legacy.`, false, {
        id: 'warp-drive:deprecate-legacy-request-methods',
        until: '6.0',
        for: '@warp-drive/core',
        url: 'https://docs.warp-drive.io/api/@warp-drive/core/build-config/deprecations/variables/ENABLE_LEGACY_REQUEST_METHODS',
        since: {
          enabled: '5.7',
          available: '5.7'
        }
      });
      return normalize.call(this, ...args);
    }
  };
  serializeRecord = (...args) => {
    if (macroCondition(!getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_REQUEST_METHODS)) {
      macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
        {
          throw new Error(`You cannot use store.serializeRecord when ENABLE_LEGACY_REQUEST_METHODS is false without explicitly registering the serializeRecord hook from @ember-data/legacy-compat or @warp-drive/legacy.`);
        }
      })() : {};
    } else {
      deprecate(`store.serializeRecord is deprecated, please use store.request to perform requests and builders/handlers/utils to produce and process them, or explicitly register the serializeRecord and serializerFor hooks from @ember-data/legacy-compat or @warp-drive/legacy.`, false, {
        id: 'warp-drive:deprecate-legacy-request-methods',
        until: '6.0',
        for: '@warp-drive/core',
        url: 'https://docs.warp-drive.io/api/@warp-drive/core/build-config/deprecations/variables/ENABLE_LEGACY_REQUEST_METHODS',
        since: {
          enabled: '5.7',
          available: '5.7'
        }
      });
      return serializeRecord.call(this, ...args);
    }
  };
  destroy() {
    cleanup.call(this);
    super.destroy();
  }
}

export { Store as default };
