(function () {
    "use strict";

    function EditController($scope, $location, $routeParams, entityResource, packageResource, contentTypeResource, templateResource, stylesheetResource, languageResource, dictionaryResource, dataTypeResource, editorService, formHelper) {

        const vm = this;

        vm.showBackButton = true;

        // open all expansion panels
        vm.propertiesOpen = true;
        vm.contentOpen = true;
        vm.filesOpen = true;
        vm.actionsOpen = true;

        vm.back = back;
        vm.createPackage = createPackage;
        vm.save = save;
        vm.removeContentItem = removeContentItem;
        vm.openContentPicker = openContentPicker;
        vm.openFilePicker = openFilePicker;
        vm.removeFile = removeFile;
        vm.openControlPicker = openControlPicker;
        vm.removeControl = removeControl;

        function onInit() {

            const packageId = $routeParams.id;
            const create = $routeParams.create;

            if(create) {
                //pre populate package with some values
                vm.package = {
                    "version": "0.0.1",
                    "license": "MIT License",
                    "licenseUrl": "http://opensource.org/licenses/MIT",
                    "umbracoVersion": Umbraco.Sys.ServerVariables.application.version
                };
            } else {
                // load package
                packageResource.getCreatedById(packageId).then(createdPackage => {
                    vm.package = createdPackage;

                    // get render model for content node
                    if(vm.package.contentNodeId) {
                        entityResource.getById(vm.package.contentNodeId, "Document")
                            .then((entity) => {
                                vm.contentNodeDisplayModel = entity;
                            });
                    }

                }, angular.noop);
            }

            // get all doc types
            contentTypeResource.getAll().then(documentTypes => {
                // a package stores the id as a string so we 
                // need to convert all ids to string for comparison
                documentTypes.forEach(documentType => {
                    documentType.id = documentType.id.toString();
                });
                vm.documentTypes = documentTypes;
            });

            // get all templates
            templateResource.getAll().then(templates => {
                // a package stores the id as a string so we 
                // need to convert all ids to string for comparison
                templates.forEach(template => {
                    template.id = template.id.toString();
                });
                vm.templates = templates;
            });

            // get all stylesheets
            stylesheetResource.getAll().then(stylesheets => {
                vm.stylesheets = stylesheets;
            });

            // TODO: implement macros
            vm.macros = [];

            // get all languages
            languageResource.getAll().then(languages => {
                // a package stores the id as a string so we 
                // need to convert all ids to string for comparison
                languages.forEach(language => {
                    language.id = language.id.toString();
                });
                vm.languages = languages;
            });

            // get all dictionary items
            dictionaryResource.getList().then(dictionaryItems => {
                // a package stores the id as a string so we 
                // need to convert all ids to string for comparison
                dictionaryItems.forEach(dictionaryItem => {
                    dictionaryItem.id = dictionaryItem.id.toString();
                });
                vm.dictionaryItems = dictionaryItems;
            });

            // get all data types items
            dataTypeResource.getAll().then(dataTypes => {
                // a package stores the id as a string so we 
                // need to convert all ids to string for comparison
                dataTypes.forEach(dataType => {
                    dataType.id = dataType.id.toString();
                });
                vm.dataTypes = dataTypes;
            });

        }

        function back() {
            $location.path("packages/packages/overview").search('create', null);;
        }

        function createPackage(editPackageForm) {

            if (formHelper.submitForm({ formCtrl: editPackageForm, scope: $scope })) {

                vm.createPackageButtonState = "busy";

                packageResource.createPackage(vm.package).then((updatedPackage) => {
                    vm.package = updatedPackage;
                    vm.createPackageButtonState = "success";
                }, function(err){
                    formHelper.handleError(err);
                    vm.createPackageButtonState = "error";
                });
            }
        }

        function save() {
            console.log("save package");
        }

        function removeContentItem() {
            vm.package.contentNodeId = null;
        }

        function openContentPicker() {
            const contentPicker = {
                submit: function(model) {
                    if(model.selection && model.selection.length > 0) {
                        vm.package.contentNodeId = model.selection[0].id.toString();
                        vm.contentNodeDisplayModel = model.selection[0];
                    }
                    editorService.close();
                },
                close: function() {
                    editorService.close();
                }
            };
            editorService.contentPicker(contentPicker);
        }

        function openFilePicker() {

            let selection = angular.copy(vm.package.files);

            const filePicker = {
                title: "Select files",
                section: "settings",
                treeAlias: "files",
                entityType: "file",
                multiPicker: true,
                onlyInitialized: false,
                select: function(node) {
                    node.selected = !node.selected;

                    const id = unescape(node.id);
                    const index = selection.indexOf(id);

                    if(node.selected) {
                        if(index === -1) {
                            selection.push(id);
                        }
                    } else {
                        selection.splice(index, 1);
                    }
                },
                submit: function() {
                    vm.package.files = selection;
                    editorService.close();
                },
                close: function() {
                    editorService.close();
                }
            };
            editorService.treePicker(filePicker);
        }

        function removeFile(index) {
            vm.package.files.splice(index, 1);
        }

        function openControlPicker() {
            const controlPicker = {
                title: "Select control",
                treeAlias: "files",
                section:"settings",
                entityType: "file",
                submit: function(model) {
                    if(model.selection && model.selection.length > 0) {
                        vm.package.control = model.selection[0];
                    }
                    editorService.close();
                },
                close: function() {
                    editorService.close();
                }
            };
            editorService.contentPicker(controlPicker);
        }

        function removeControl() {
            vm.package.control = null;
        }

        onInit();

    }

    angular.module("umbraco").controller("Umbraco.Editors.Packages.EditController", EditController);

})();

@@ -7157,16 +7157,53 @@
 ntrol%22,%0A
+                section: %22settings%22,%0A
         
@@ -7250,25 +7250,25 @@
     
-section:%22settings
+entityType: %22file
 %22,%0A 
@@ -7282,34 +7282,38 @@
         
-entityType: %22file%22
+onlyInitialized: false
 ,%0A      
@@ -7315,36 +7315,36 @@
                s
-ubmi
+elec
 t: function(mode
@@ -7331,37 +7331,36 @@
 elect: function(
-m
+n
 ode
-l
 ) %7B%0A            
@@ -7371,61 +7371,38 @@
     
-if(model.selection && model.selection.length %3E 0) %7B%0A 
+const id = unescape(node.id);%0A
     
@@ -7409,35 +7409,32 @@
                 
-   
 vm.package.contr
@@ -7420,33 +7420,37 @@
      vm.package.
-c
+loadC
 ontrol = model.s
@@ -7446,49 +7446,11 @@
 l = 
-model.selection%5B0%5D;%0A                    %7D
+id;
 %0A   
@@ -7638,39 +7638,36 @@
   editorService.
-content
+tree
 Picker(controlPi
@@ -7742,17 +7742,21 @@
 package.
-c
+loadC
 ontrol =

fix control picker