var applescript, tellTo;

applescript = require('applescript');

tellTo = 'tell application "System Events" to ';

module.exports = {
  enable: function(opts) {
    return new Promise(function(resolve, reject) {
      var command, isHidden, properties;
      isHidden = opts.isHiddenOnLaunch ? 'true' : 'false';
      properties = "{path:\"" + opts.appPath + "\", hidden:" + isHidden + ", name:\"" + opts.appName + "\"}";
      command = tellTo + " make login item at end with properties " + properties;
      return applescript.execString(command, function(err) {
        if (err != null) {
          return reject(err);
        }
        return resolve();
      });
    });
  },
  disable: function(opts) {
    return new Promise(function(resolve, reject) {
      var command;
      command = tellTo + ("delete login item \"" + opts.appName + "\"");
      return applescript.execString(command, function(err) {
        if (err != null) {
          return reject(err);
        }
        return resolve();
      });
    });
  },
  isEnabled: function(opts) {
    return new Promise(function(resolve, reject) {
      var command;
      command = tellTo + "get the name of every login item";
      return applescript.execString(command, function(err, loginItems) {
        var isPresent;
        if (err != null) {
          return reject(err);
        }
        isPresent = loginItems != null ? loginItems.indexOf(opts.appName) : void 0;
        return resolve((isPresent != null) && isPresent !== -1);
      });
    });
  }
};
