import './main.scss';
import Craft from 'craft';
import EmbeddedAssets from './classes/EmbeddedAssets';
import Button from './classes/Button';
import { monkeypatch } from './utilities';

const embeddedAssets = new EmbeddedAssets();

monkeypatch(Craft.AssetIndex, 'init', function () {
    const button = new Button();

    const $uploadButton = this.$uploadButton;
    const inHeader = $uploadButton.closest('#header').length > 0;
    const inModal = $uploadButton.closest('.modal').length > 0;

    let modalOrientations;

    if (inHeader) {
        this.$uploadButton.before(button.$element);
        modalOrientations = ['bottom', 'left', 'right', 'top'];
    } else if (inModal) {
        this.$uploadButton.after(button.$element);
        modalOrientations = ['top', 'right', 'bottom', 'left'];
    }

    const getFolderId = () => {
        const split = this.getDefaultSourceKey().split(':');

        if (split[split.length - 1]) {
            return split[split.length - 1];
        }

        return 0;
    };

    embeddedAssets.addButton(button, modalOrientations, getFolderId);

    let idsToSelect = [];

    embeddedAssets.on('save', (e) => {
        idsToSelect.push(e.assetId);
        this.updateElements();
    });

    this.on('updateElements', () => {
        idsToSelect.forEach(id => this.view.selectElementById(id));
        idsToSelect = [];
    });
});

window.EmbeddedAssets = embeddedAssets;

@@ -13,17 +13,16 @@
 in.scss'
-;
 %0Aimport 
@@ -39,17 +39,16 @@
  'craft'
-;
 %0Aimport 
@@ -93,17 +93,16 @@
 dAssets'
-;
 %0Aimport 
@@ -131,17 +131,16 @@
 /Button'
-;
 %0Aimport 
@@ -173,17 +173,16 @@
 ilities'
-;
 %0A%0Aconst 
@@ -218,17 +218,16 @@
 Assets()
-;
 %0A%0Amonkey
@@ -270,18 +270,14 @@
 tion
- 
 ()
- %7B%0A    
+%0A%7B%0A%09
 cons
@@ -299,23 +299,19 @@
 Button()
-;
 %0A%0A
-    
+%09
 const $u
@@ -342,22 +342,18 @@
 adButton
-;%0A    
+%0A%09
 const in
@@ -404,22 +404,18 @@
 ngth %3E 0
-;%0A    
+%0A%09
 const in
@@ -464,23 +464,19 @@
 ngth %3E 0
-;
 %0A%0A
-    
+%09
 let moda
@@ -488,23 +488,19 @@
 ntations
-;
 %0A%0A
-    
+%09
 if (inHe
@@ -504,27 +504,22 @@
 nHeader)
- %7B%0A        
+%0A%09%7B%0A%09%09
 this.$up
@@ -548,34 +548,27 @@
 on.$element)
-;%0A        
+%0A%09%09
 modalOrienta
@@ -613,16 +613,13 @@
 op'%5D
-;%0A    %7D 
+%0A%09%7D%0A%09
 else
@@ -631,27 +631,22 @@
 inModal)
- %7B%0A        
+%0A%09%7B%0A%09%09
 this.$up
@@ -678,26 +678,19 @@
 element)
-;%0A        
+%0A%09%09
 modalOri
@@ -739,21 +739,14 @@
 ft'%5D
-;%0A    %7D%0A%0A    
+%0A%09%7D%0A%0A%09
 cons
@@ -769,24 +769,18 @@
 () =%3E %7B%0A
-        
+%09%09
 const sp
@@ -861,16 +861,24 @@
 th - 1%5D)
+%0A       
  %7B%0A     
@@ -949,20 +949,14 @@
  0;%0A
-    %7D;%0A%0A    
+%09%7D;%0A%0A%09
 embe
@@ -1015,23 +1015,19 @@
 olderId)
-;
 %0A%0A
-    
+%09
 let idsT
@@ -1038,23 +1038,19 @@
 ect = %5B%5D
-;
 %0A%0A
-    
+%09
 embedded
@@ -1071,25 +1071,18 @@
 e', 
-(e) =%3E %7B%0A        
+e =%3E%0A%09%7B%0A%09%09
 idsT
@@ -1104,26 +1104,19 @@
 assetId)
-;%0A        
+%0A%09%09
 this.upd
@@ -1132,23 +1132,15 @@
 ts()
-;%0A    
+%0A%09
 %7D)
-;
 %0A%0A
-    
+%09
 this
@@ -1166,27 +1166,22 @@
 ', () =%3E
- %7B%0A        
+%0A%09%7B%0A%09%09
 idsToSel
@@ -1192,18 +1192,20 @@
 forEach(
+(
 id
+)
  =%3E this
@@ -1232,26 +1232,19 @@
 yId(id))
-;%0A        
+%0A%09%09
 idsToSel
@@ -1255,21 +1255,15 @@
 = %5B%5D
-;%0A    
+%0A%09
 %7D)
-;
 %0A%7D)
-;
 %0A%0Awi
@@ -1298,10 +1298,8 @@
 edAssets
-;%0A

Revert style changes on client main.js.