// Generated by CoffeeScript 1.7.1
(function() {
  var BrowserWindow, MenuItem, nextCommandId, v8Util;

  BrowserWindow = require('browser-window');

  v8Util = process.atomBinding('v8_util');

  nextCommandId = 0;

  MenuItem = (function() {
    MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'];

    function MenuItem(options) {
      var Menu, click, _ref;
      Menu = require('menu');
      click = options.click, this.selector = options.selector, this.type = options.type, this.label = options.label, this.sublabel = options.sublabel, this.accelerator = options.accelerator, this.enabled = options.enabled, this.visible = options.visible, this.checked = options.checked, this.submenu = options.submenu;
      if ((this.type == null) && (this.submenu != null)) {
        this.type = 'submenu';
      }
      if (this.type === 'submenu' && ((_ref = this.submenu) != null ? _ref.constructor : void 0) !== Menu) {
        throw new Error('Invalid submenu');
      }
      this.overrideReadOnlyProperty('type', 'normal');
      this.overrideReadOnlyProperty('accelerator');
      this.overrideReadOnlyProperty('submenu');
      this.overrideProperty('label', '');
      this.overrideProperty('sublabel', '');
      this.overrideProperty('enabled', true);
      this.overrideProperty('visible', true);
      this.overrideProperty('checked', false);
      if (MenuItem.types.indexOf(this.type) === -1) {
        throw new Error("Unknown menu type " + this.type);
      }
      this.commandId = ++nextCommandId;
      this.click = (function(_this) {
        return function() {
          var _ref1;
          if ((_ref1 = _this.type) === 'checkbox' || _ref1 === 'radio') {
            _this.checked = !_this.checked;
          }
          if (typeof click === 'function') {
            return click(_this, BrowserWindow.getFocusedWindow());
          } else if (typeof _this.selector === 'string') {
            return Menu.sendActionToFirstResponder(_this.selector);
          }
        };
      })(this);
    }

    MenuItem.prototype.overrideProperty = function(name, defaultValue) {
      if (defaultValue == null) {
        defaultValue = null;
      }
      return this[name] != null ? this[name] : this[name] = defaultValue;
    };

    MenuItem.prototype.overrideReadOnlyProperty = function(name, defaultValue) {
      if (defaultValue == null) {
        defaultValue = null;
      }
      if (this[name] == null) {
        this[name] = defaultValue;
      }
      return Object.defineProperty(this, name, {
        enumerable: true,
        writable: false,
        value: this[name]
      });
    };

    return MenuItem;

  })();

  module.exports = MenuItem;

}).call(this);
