import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';

export class SplitTransactionTabExpand extends Feature {
  shouldInvoke() {
    return isCurrentRouteAccountsPage() && !!$('.ynab-grid-split-add-sub-transaction').length;
  }

  invoke() {}

  applyNewTabBehavior(event) {
    // If tab was pressed, simulate a mouse click on the "Add another split" button
    if (event.keyCode === 9) {
      let addSplitButton = $('.ynab-grid-split-add-sub-transaction');
      if (addSplitButton.length !== 0) {
        // The YNAB app checks the detail property isn't 0, so .click() won't work
        const clickEvent = new jQuery.Event('click', { detail: 1 });
        addSplitButton.trigger(clickEvent);
      }
    }
  }

  observe() {
    if (!this.shouldInvoke()) return;

    const lastInput = $('.ynab-grid-body-row.is-editing input').last();

    if (!lastInput.attr('data-toolkit-tab-expand')) {
      lastInput.attr('data-toolkit-tab-expand', true);
      lastInput.on('keydown', this.applyNewTabBehavior);
    }
  }
}

@@ -58,83 +58,8 @@
 re';
-%0Aimport %7B isCurrentRouteAccountsPage %7D from 'toolkit/extension/utils/ynab';
 %0A%0Aex
@@ -115,23 +115,17 @@
 ure %7B%0A  
-shouldI
+i
 nvoke() 
@@ -134,116 +134,489 @@
     
-return isCurrentRouteAccountsPage() && !!$('.ynab-grid-split-add-sub-transaction').length;%0A  %7D%0A%0A  invoke() %7B
+this.addToolkitEmberHook('register/grid-split', 'didRender', this.addEventListeners);%0A  %7D%0A%0A  addEventListeners() %7B%0A    const previousInputs = $('input%5Bdata-toolkit-tab-expand%5D');%0A    previousInputs.removeAttr('data-toolkit-tab-expand');%0A    previousInputs.off('keydown', this.applyNewTabBehavior);%0A%0A    const lastInput = $('.ynab-grid-body-row.is-editing input').last();%0A    lastInput.attr('data-toolkit-tab-expand', true);%0A    lastInput.on('keydown', this.applyNewTabBehavior);%0A  
 %7D%0A%0A 
@@ -756,16 +756,35 @@
 de === 9
+ && !event.shiftKey
 ) %7B%0A    
@@ -1107,310 +1107,7 @@
 %0A  %7D
-%0A%0A  observe() %7B%0A    if (!this.shouldInvoke()) return;%0A%0A    const lastInput = $('.ynab-grid-body-row.is-editing input').last();%0A%0A    if (!lastInput.attr('data-toolkit-tab-expand')) %7B%0A      lastInput.attr('data-toolkit-tab-expand', true);%0A      lastInput.on('keydown', this.applyNewTabBehavior);%0A    %7D%0A  %7D
 %0A%7D%0A

change observe to hook, fix shift+tab, remove old listeners (#2516)