System.registerModule("api.js", [], function() {
  "use strict";
  var __moduleName = "api.js";
  var Secret = function() {
    function Secret(cipher, data) {}
    return ($traceurRuntime.createClass)(Secret, {setData: function(clearData, passphrase) {}}, {});
  }();
  var SecretType = function() {
    function SecretType(data) {}
    return ($traceurRuntime.createClass)(SecretType, {}, {});
  }();
  var Keychain = function() {
    function Keychain(data) {}
    return ($traceurRuntime.createClass)(Keychain, {}, {});
  }();
  var User = function() {
    function User(data) {
      this.email = data.email;
      this.first_name = data.first_name;
      this.last_name = data.last_name;
      this.username = data.username;
      this.id = data.id;
    }
    return ($traceurRuntime.createClass)(User, {}, {});
  }();
  function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
      return response;
    } else {
      var error = new Error(response.statusText);
      error.response = response;
      throw error;
    }
  }
  function parseJSON(response) {
    console.log('#===>', response);
    return response.json();
  }
  function post(url, data) {
    var token = arguments[2] !== (void 0) ? arguments[2] : null;
    var formData = new FormData();
    formData.append('username', data.username);
    formData.append('password', data.password);
    var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
    if (token) {
      headers['Authorization'] = 'Bearer ' + token;
    }
    return fetch(url, {
      method: 'post',
      headers: headers,
      body: 'username=' + data.username + '&password=' + data.password
    });
  }
  function postJSON(url, data) {
    var token = arguments[2] !== (void 0) ? arguments[2] : null;
    var headers = {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    };
    if (token) {
      url += '?bearer=' + token;
    }
    return fetch(url, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(data)
    }).then(parseJSON);
  }
  function fetchJSON(url) {
    var token = arguments[1] !== (void 0) ? arguments[1] : null;
    var headers = {'Accept': 'application/json'};
    if (token) {
      url += '?bearer=' + token;
    }
    return fetch(url, {
      method: 'GET',
      headers: headers
    }).then(parseJSON);
  }
  var API = function() {
    function API(url) {
      var model = arguments[1] !== (void 0) ? arguments[1] : null;
      var token = arguments[2] !== (void 0) ? arguments[2] : {};
      if (!this.root) {
        this.root = url;
      }
      this.url = url;
      this.model = model;
      this.token = token;
    }
    return ($traceurRuntime.createClass)(API, {
      signin: function(infos) {
        var self = this;
        return post(this.root + '/signin', infos).then(checkStatus).then(parseJSON).then(function(json) {
          console.log('json : ', json);
          if (json.token) {
            self.token.value = json.token;
          }
          return json.token;
        });
      },
      all: function(model) {
        return new API(this.url + '/' + model, model, this.token);
      },
      one: function(model, id) {
        return new API(this.url + '/' + model + '/' + id, model, this.token);
      },
      buildObject: function(model, data) {
        console.log('model : "' + model + '"');
        switch (model) {
          case 'secrets':
            return new Secret(self.cipher, data);
          case 'secret-types':
            break;
          case 'users':
            console.log('ici...');
            return new User(data);
            break;
          case 'keychains':
            break;
          case 'groups':
            break;
          case 'oganisations':
            break;
        }
      },
      getList: function() {
        if (!this.model) {
          throw new Error("no model defined...");
        }
        var self = this;
        return;
        fetchJSON(this.url, this.token.value).then(function(objects) {
          var models = [];
          objects.forEach(function(object) {
            models.push(self.buildObject(self.model, object));
          });
          return models;
        });
      },
      get: function() {
        var self = this;
        return fetchJSON(this.url, this.token.value).then(function(object) {
          return self.buildObject(self.model, object);
        });
      },
      save: function(object) {
        var self = this;
        var action = null;
        if (object.id) {
          action = put(this.url, object, this.token.value);
        } else {
          action = postJSON(this.url, object, this.token.value);
        }
        return action.then(function(returnedObject) {
          return self.buildObject(self.model, returnedObject);
        });
      },
      remove: function(object) {
        var self = this;
        return remove(this.url, object, this.token.value).then(function(returnedObject) {
          return self.buildObject(self.model, returnedObject);
        });
      }
    }, {});
  }();
  ;
  return {
    get Secret() {
      return Secret;
    },
    get SecretType() {
      return SecretType;
    },
    get Keychain() {
      return Keychain;
    },
    get User() {
      return User;
    },
    get API() {
      return API;
    }
  };
});
System.registerModule("index.js", [], function() {
  "use strict";
  var __moduleName = "index.js";
  var $__0 = System.get("api.js"),
      API = $__0.API,
      User = $__0.User;
  var Keychain = function($__super) {
    function Keychain(url) {
      $traceurRuntime.superConstructor(Keychain).call(this, url);
    }
    return ($traceurRuntime.createClass)(Keychain, {}, {}, $__super);
  }(API);
  var api = new API('http://localhost/secure-keychain-php-backend/web/app_dev.php');
  document.querySelector('form').addEventListener('submit', function(e) {
    e.preventDefault();
    e.stopPropagation();
    api.signin({
      username: document.querySelector('#username').value,
      password: document.querySelector('#password').value
    }).then(function(token) {
      console.log('logged...');
      var newUser = new User({
        email: 'test@test.com',
        first_name: 'test1',
        last_name: 'test2',
        username: 'test'
      });
      console.log('object created');
      newUser = api.all('users').save(newUser);
      console.log('request sent');
    }).catch(function(e) {
      console.log('not logged...', e);
    });
    return false;
  });
  return {};
});
System.get("index.js" + '');
