import app from '../app';
import BaseModel from './BaseModel';
import ShippingAddresses from '../collections/ShippingAddresses';
import AppearanceSettings from '../models/AppearanceSettings';
import TransactionSettings from '../models/TransactionSettings';
import ServerSettings from '../models/ServerSettings';
import SMTPSettings from '../models/SMTPSettings';
import SettingsAPIAdaptor from '../utils/settingsAPIAdaptor';

export default class extends BaseModel {
  defaults() {
    return {
      paymentDataInQR: false,
      showNotifications: true,
      showNsfw: true,
      localCurrency: 'USD',
      country: 'UNITED_STATES',
      language: 'en-US',
      termsAndConditions: '',
      refundPolicy: '',
      blockedNodes: [],
      storeModerators: [],
    };
  }

  url() {
    return app.getServerUrl('ob/settings/');
  }

  get nested() {
    return {
      shippingAddresses: ShippingAddresses,
      appearanceSettings: AppearanceSettings,
      transactionSettings: TransactionSettings,
      serverSettings: ServerSettings,
      smtpSettings: SMTPSettings
    };
  }

  parse( response ) {
    const data = this.fromAPIFormatJSON( response );
    // make it immutable to highlight unintended modification errors
    Object.freeze( data );
    return data;
  }

  toModelFormatJSON( ) {
    return SettingsAPIAdaptor.convertAPIToModelFormat( super.toJSON( ) );
  }

  toAPIFormatJSON( ) {
    const raw = super.toJSON( );
    return SettingsAPIAdaptor.convertModelToAPIFormat( raw );
  }

  toJSON( ) {
    const apiFormat = this.toAPIFormatJSON( );
    // make it immutable to highlight unintended modification errors
    Object.freeze( apiFormat );
    return apiFormat;
  }

  fromAPIFormatJSON( response ) {
    return SettingsAPIAdaptor.convertAPIToModelFormat( response );
  }

  sync(method, model, options) {
    if (method === 'create' && typeof options.type === 'undefined') {
      // we will use PUT unless you explicitly save with POST,
      // e.g. model.save({}, { type: 'POST' })
      options.type = 'PUT';
    }

    return super.sync(method, model, options);
  }
}

@@ -359,70 +359,8 @@
 gs';
-%0Aimport SettingsAPIAdaptor from '../utils/settingsAPIAdaptor';
 %0A%0Aex
@@ -1101,104 +1101,8 @@
  );%0A
-    // make it immutable to highlight unintended modification errors%0A    Object.freeze( data );%0A
     
@@ -1158,52 +1158,8 @@
 turn
- SettingsAPIAdaptor.convertAPIToModelFormat(
  sup
@@ -1168,18 +1168,16 @@
 .toJSON(
- )
  );%0A  %7D%0A
@@ -1247,58 +1247,12 @@
 turn
- SettingsAPIAdaptor.convertModelToAPIFormat(
  raw
- )
 ;%0A  
@@ -1319,109 +1319,8 @@
  );%0A
-    // make it immutable to highlight unintended modification errors%0A    Object.freeze( apiFormat );%0A
     
@@ -1390,52 +1390,8 @@
 turn
- SettingsAPIAdaptor.convertAPIToModelFormat(
  res
@@ -1391,26 +1391,24 @@
 urn response
- )
 ;%0A  %7D%0A%0A  syn

Revert some changes and app does run.