(function() {
  var permissionsContent, pointerContent;

  $.extend(decko.editorContentFunctionMap, {
    'select.pointer-select': function() {
      return pointerContent(this.val());
    },
    'select.pointer-multiselect': function() {
      return pointerContent(this.val());
    },
    '.pointer-radio-list': function() {
      return pointerContent(this.find('input:checked').val());
    },
    '.pointer-list-ul': function() {
      return pointerContent(this.find('input').map(function() {
        return $(this).val();
      }));
    },
    '.pointer-checkbox-list': function() {
      return pointerContent(this.find('input:checked').map(function() {
        return $(this).val();
      }));
    },
    '.pointer-select-list': function() {
      return pointerContent(this.find('.pointer-select select').map(function() {
        return $(this).val();
      }));
    },
    '._pointer-filtered-list': function() {
      return pointerContent(this.find('._filtered-list-item').map(function() {
        return $(this).data('cardName');
      }));
    },
    '._pointer-list': function() {
      return pointerContent(this.find('._pointer-item').map(function() {
        return $(this).val();
      }));
    },
    '.perm-editor': function() {
      return permissionsContent(this);
    }
  });

  decko.editorInitFunctionMap['.pointer-list-editor'] = function() {
    this.sortable({
      handle: '.handle',
      cancel: ''
    });
    return decko.initPointerList(this.find('input'));
  };

  decko.editorInitFunctionMap['._pointer-filtered-list'] = function() {
    return this.sortable({
      handle: '._handle',
      cancel: ''
    });
  };

  $.extend(decko, {
    initPointerList: function(input) {
      return decko.initAutoCardPlete(input);
    },
    initAutoCardPlete: function(input) {
      var optionsCard, path;
      optionsCard = input.data('options-card');
      if (!optionsCard) {
        return;
      }
      path = optionsCard + '.json?view=name_match';
      return input.autocomplete({
        source: decko.slotPath(path)
      });
    },
    pointerContent: function(vals) {
      var list;
      list = $.map($.makeArray(vals), function(v) {
        if (v) {
          return '[[' + v + ']]';
        }
      });
      return $.makeArray(list).join("\n");
    }
  });

  pointerContent = function(vals) {
    return decko.pointerContent(vals);
  };

  permissionsContent = function(ed) {
    var groups, indivs;
    if (ed.find('#inherit').is(':checked')) {
      return '_left';
    }
    groups = ed.find('.perm-group input:checked').map(function() {
      return $(this).val();
    });
    indivs = ed.find('.perm-indiv input').map(function() {
      return $(this).val();
    });
    return pointerContent($.makeArray(groups).concat($.makeArray(indivs)));
  };

  $(window).ready(function() {
    $('body').on('click', '.pointer-item-add', function(event) {
      decko.addPointerItem(this);
      return event.preventDefault();
    });
    $('body').on('keydown', '.pointer-item-text', function(event) {
      if (event.key === 'Enter') {
        decko.addPointerItem(this);
        return event.preventDefault();
      }
    });
    $('body').on('keyup', '.pointer-item-text', function(_event) {
      return decko.updateAddItemButton(this);
    });
    return $('body').on('click', '.pointer-item-delete', function() {
      var item;
      item = $(this).closest('li');
      if (item.closest('ul').find('.pointer-li').length > 1) {
        return item.remove();
      } else {
        return item.find('input').val('');
      }
    });
  });

  decko.slotReady(function(slot) {
    return slot.find('.pointer-list-editor').each(function() {
      return decko.updateAddItemButton(this);
    });
  });

  $.extend(decko, {
    addPointerItem: function(el) {
      var newInput;
      newInput = decko.nextPointerInput(decko.lastPointerItem(el));
      newInput.val('');
      newInput.focus();
      decko.updateAddItemButton(el);
      return decko.initPointerList(newInput);
    },
    nextPointerInput: function(lastItem) {
      var lastInput, newItem;
      lastInput = lastItem.find('input');
      if (lastInput.val() === '') {
        return lastInput;
      }
      newItem = lastItem.clone();
      lastItem.after(newItem);
      return newItem.find('input');
    },
    lastPointerItem: function(el) {
      return $(el).closest('.content-editor').find('.pointer-li:last');
    },
    updateAddItemButton: function(el) {
      var button, disabled;
      button = $(el).closest('.content-editor').find('.pointer-item-add');
      disabled = decko.lastPointerItem(el).find('input').val() === '';
      return button.prop('disabled', disabled);
    }
  });

}).call(this);