/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * ___________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/


/*global $, Folder, DocumentColorSpace, app, File, SaveOptions, ImageColorSpace, ColorConvertPurpose, ExportOptionsSVG, SVGFontSubsetting, SVGFontType, ExportType, JSXGlobals, CMYKColor, GrayColor, LabColor, TEXT, UTIL, COLOR, ExportOptionsPNG24, Dimensions, OpenOptions, ColorModel, PATTERN */


var extensionPath = $.fileName.split('/').slice(0, -1).join('/') + '/';
$.evalFile(extensionPath + 'util.jsx');
$.evalFile(extensionPath + 'color.jsx');
$.evalFile(extensionPath + 'text.jsx');
$.evalFile(extensionPath + 'material.jsx');
$.evalFile(extensionPath + 'gradient.jsx');
$.evalFile(extensionPath + 'pattern.jsx');


function padZero(str) {
    return "000000".substr(str.length) + str;
}

function getThumbnailOptions(maxWidth, maxHeight) {
    var options = new ExportOptionsPNG24();
    options.antiAliasing = true;
    options.transparency = true;
    var dimn = new Dimensions();
    dimn.width = maxWidth ? maxWidth : JSXGlobals.previewMaxWidth;
    dimn.height = maxHeight ? maxHeight : JSXGlobals.previewMaxHeight;
    options.dimensions = dimn;

    return options;
}

function areEqual(colorData1, colorData2) {
    return colorData1 && colorData2 && JSON.stringify(colorData1[0].value) === JSON.stringify(colorData2[0].value);
}

function pushUnique(colorData, colorType, layerColors) {
    if (colorData === undefined) {
        return;
    }
    var index;
    for (index = 0; index < layerColors.length; index++) {
        if (areEqual(layerColors[index].data, colorData)) {
            return;
        }
    }
    layerColors.push({
        'colorType': colorType,
        'data': colorData
    });
}


function populateTextOptionsInLayerObject(layerObject, selection, fillTooltipObj, strokeTooltipObj, isFontAvailableinAIObj) {
    layerObject.kind = "LayerKind.TEXT"; /// kind need to be Layerkind.Text to enable the button
    if (selection.contents === "") {
        layerObject.text = "Random Text"; ///For enabling 'Add Text Style'
    } else {
        layerObject.text = selection.contents;
        var maxLength = 100;
        if (layerObject.text.length > maxLength)
            layerObject.text = layerObject.text.substr(0, maxLength - 1);
    }
    fillTooltipObj.fillTooltip = JSXGlobals.AI_TEXT_FILL;
    strokeTooltipObj.strokeTooltip = JSXGlobals.AI_TEXT_STROKE;
    try {
        layerObject.canAddTextAsset = true;
        if (selection && selection.length > 0 && selection.characterAttributes && selection.characterAttributes.textFont) {
            isFontAvailableinAIObj.isFontAvailableinAI = app.textFonts.isFontAvailable(selection.characterAttributes.textFont.name);
        } else if (selection && selection.length === 0) {
            var currentFont = app.textFonts.getCurrentFont();
            isFontAvailableinAIObj.isFontAvailableinAI = app.textFonts.isFontAvailable(currentFont);
            layerObject.canAddTextAsset = true;
            layerObject.insertionPointExists = true;
        }
    } catch (ignore) {

    }
}

$._ADBE_LIBS_ILST = {
    loadAndSelectBrush: function (filePath, brushName) {
        try {
            app.activeDocument.brushes.add(new File(filePath), brushName);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-loadAndSelectBrush()', ex);
        }
    },
    replaceColor: function (colorData) {
        try {
            var originalColor = COLOR.dataToSolidColor(colorData);
            var modifiedColor = app.showColorPicker(originalColor);
            JSXGlobals.colorModifiedByUser = modifiedColor;
            return (originalColor !== modifiedColor);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-replaceColor()', ex);
        }
    },
    replaceTextStyle: TEXT.replaceTextStyle,
    isFontAvailable: TEXT.isFontAvailable,
    getSelectedSwatchList: function () {
        try {
            var activeDoc = app.activeDocument;
            var result = [];
            var i;
            if (activeDoc) {
                var swatches = activeDoc.swatches;
                if (swatches) {
                    var swatchList = swatches.getSelected(false);
                    if (swatchList && swatchList.length) {
                        for (i = 0; i < swatchList.length; i++) {
                            if (swatchList[i].color.typename === 'PatternColor') {
                                result.push({
                                    'name': swatchList[i].name,
                                    'data': PATTERN.patternColorToData(swatchList[i].color),
                                    'typename': swatchList[i].color.typename
                                });
                            } else if (swatchList[i].color.typename === 'GradientColor') {
                                result.push({
                                    'name': swatchList[i].name,
                                    'data': GRADIENT.gradientColorToData(swatchList[i].color),
                                    'typename': swatchList[i].color.typename
                                });
                            } else {
                                result.push({
                                    'name': swatchList[i].name,
                                    'data': COLOR.solidColorToData(swatchList[i].color),
                                    'typename': swatchList[i].color.typename
                                });
                            }
                        }
                    }
                }
            }
            return JSON.stringify(result);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getSelectedSwatchList()', ex);
        }
    },
    getSelectedMaterialList: function () {
        try {
            var activeDoc = app.activeDocument;
            var result = [];
            var i;
            if (activeDoc) {
                var material = activeDoc.materials;
                if (material) {
                    var materialList = material.getSelectedMaterials();
                    if (materialList && materialList.length) {
                        for (i = 0; i < materialList.length; i++) {
                            result.push({
                                'name': materialList[i].name,
                                'type': 'application/vnd.adobe.element.material+dcx',
                                'representations': MATERIAL.materialToData(materialList[i])
                            });
                        }
                    }
                }
            }
            return JSON.stringify(result);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getSelectedMaterialList()', ex);
        }
    },
    getSelectedSwatchGroupList: function () {
        try {
            var activeDoc = app.activeDocument;
            var result = [];
            var i;
            var j;
            var swatchMoodKey = "mood";
            var swatchCollectionkey = "swatches";
            var swatchGroupList;
            var allSwatchesInColorTheme;

            var colorThemeObj;
            var swatchCollection;
            var swatchObj;
            var colorObj;
            if (activeDoc) {
                var swatchGroups = activeDoc.swatchGroups;
                if (swatchGroups) {
                    swatchGroupList = swatchGroups.getSelected();
                    if (swatchGroupList && swatchGroupList.length) {
                        for (i = 0; i < swatchGroupList.length; i++) {
                            colorThemeObj = {};
                            swatchCollection = [];
                            colorThemeObj[swatchMoodKey] = swatchGroupList[i].name;
                            allSwatchesInColorTheme = swatchGroupList[i].getAllSwatches();
                            for (j = 0; j < allSwatchesInColorTheme.length; j++) {
                                swatchObj = allSwatchesInColorTheme[j];
                                colorObj = COLOR.solidColorToData(swatchObj.color);
                                if (colorObj) {
                                    swatchCollection.push(colorObj);
                                }
                            }

                            colorThemeObj[swatchCollectionkey] = swatchCollection;
                            result.push(colorThemeObj);
                        }
                    }
                }
            }
            return JSON.stringify(result);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getSelectedSwatchList()', ex);
        }
    },
    placeAsset: function (filePath, libraryName, itemName, elementRef, modifiedTime, creationTime, adobeStockId, adobeStockLicense, isLinked, isOpenIn) {
        // https://forums.adobe.com/message/4896032
        // Place doesn't necessarily give you the exact artwork that is in the file,
        // copy/paste is the only way to get this.
        try {
            var assetFile = new File(filePath);
            if (!app || app.documents.length === 0 || !app.activeDocument) {
                if (isOpenIn === true) {
                    app.documents.add();
                } else {
                    // Need to make a copy of the file to avoid overwrite
                    var fileExtension = filePath.substr(filePath.lastIndexOf(".") + 1).toLowerCase();
                    var destPath = Folder.temp.fsName + '/' + new Date().valueOf() + '.' + fileExtension;

                    assetFile.copy(destPath);
                    $._ADBE_LIBS_ILST.openAsset(destPath);
                    return;
                }
            }
            var shouldLoadToPlaceGun = !isOpenIn;
            app.activeDocument.importFile(assetFile, isLinked, libraryName, itemName, elementRef,
                modifiedTime, creationTime, adobeStockId, adobeStockLicense, shouldLoadToPlaceGun);

        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-placeAsset()', ex);
        }
    },
    openAsset: function (filePath) {
        try {
            var newDoc = app.open(new File(filePath));
            return newDoc.name;
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-openAsset()', ex);
        }
    },
    openAssetForEdit: function (filePath, thumbnailPath, assetType) {
        if (thumbnailPath) {
            try {
                var options = new OpenOptions();
                options.addToRecentFiles = false;
                var newDoc = app.openCloudLibraryAssetForEditing(new File(filePath), new File(thumbnailPath), assetType, options);
                return newDoc.name;
            } catch (ex) {
                $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-openAssetForEdit()', ex);
            }
        } else {
            return $._ADBE_LIBS_ILST.openAsset(filePath);
        }
    },
    getUpdatedTextInfo: function (path) {
        try {
            var textObj = TEXT.getUpdatedTextInfo(path);
            return JSON.stringify(textObj);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getUpdatedTextInfo()', ex);
        }
    },
    openDocumentFromTemplate: function (templatePath, templateName) {
        try {
            app.open(new File(templatePath));
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-openDocumentFromTemplate()', ex);
        }
    },
    addSwatchGroup: function (colorTheme, groupName) {
        if (colorTheme) {
            try {
                var activeDoc = app.activeDocument;
                var j;
                var swatchMoodKey = "mood";
                var swatchCollectionkey = "swatches";
                var swatchObj;
                var swatchGroupObj;
                var colorObj;
                var colorCollection;
                var colorRep;

                if (activeDoc) {
                    var swatchGroups = activeDoc.swatchGroups;
                    if (swatchGroups) {
                        swatchGroupObj = swatchGroups.add();
                        swatchGroupObj.name = groupName || colorTheme[swatchMoodKey];

                        colorCollection = colorTheme[swatchCollectionkey];
                        for (j = 0; j < colorCollection.length; j++) {
                            colorObj = COLOR.getBestColorRepresentation(colorCollection[j]);
                            colorRep = COLOR.dataToSolidColor(colorObj);
                            if (colorObj.type === "spot") {
                                swatchGroupObj.addSpot(colorRep.spot);
                            } else {
                                swatchObj = activeDoc.swatches.add();
                                swatchObj.color = colorRep;
                                swatchObj.name = COLOR.getDisplayName(colorObj);
                                swatchGroupObj.addSwatch(swatchObj);
                            }
                        }
                    }
                }
            } catch (ex) {
                $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-addSwatchGroup()', ex);
                return 'err';
            }
        }
    },
    openSwatchPanel: function () {
        try {
            app.sendScriptMessage("AdobeSwatch_", "Open Swatches Panel", "");
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-openSwatchPanelopen()', ex);
        }
    },
    setColor: COLOR.setColor,
    addColor: COLOR.addColor,
    setFillColor: COLOR.setFillColor,
    setStrokeColor: COLOR.setStrokeColor,
    setFont: TEXT.setFont,
    createTextLayer: TEXT.createTextLayer,
    getCurrentState: function () {
        try {
            if (app.documents.length > 0) {
                //if (app.selection && app.selection.length > 0) {
                //var selectedLayer = app.activeDocument.activeLayer;

                var docPath = app.activeDocument.name;
                if (app.activeDocument.fullName) {
                    docPath = app.activeDocument.fullName.fsName;
                }
                /*var pageItems = selectedLayer.pageItems;
                var fullSelection = "";
                var i;
                for (i = 0; i < pageItems.length; i++) {
                    if (pageItems[i].selected === true) {
                        fullSelection += $._ADBE_LIBS_ILST.getItemID(pageItems[i]) + pageItems[i].typename + ", ";
                    }
                }
                var layerID = $._ADBE_LIBS_ILST.getItemID(app.selection[0]) + "_" + $._ADBE_LIBS_ILST.getItemID(selectedLayer) + "===" + fullSelection;*/

                //We are not using the LayerID anymore. Hence hardcode it.
                // TODO: Safely get rid of layerID after MAX
                return JSON.stringify({
                    'path': docPath,
                    'layerID': 2
                });
                //}
            }
            return JSON.stringify({
                'path': '',
                'layerID': -1
            });
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getCurrentState()', ex);
        }
    },
    getItemID: function (layer) {
        try {
            var layerID = layer.name;
            if (Object.prototype.hasOwnProperty.call(layer, "zOrderPosition")) {
                try {
                    layerID += "_" + layer.zOrderPosition;
                } catch (ignore) {}
            }
            return layerID;
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getItemID()', ex);
        }
    },
    getLayerName: function () {
        try {
            var finalName = '';
            // If the selection has exactly one item, and that item has a name, then return that.
            // Otherwise, return the empty string.
            var appSelection = app.selection;
            if (appSelection.length === 1 && appSelection[0] && appSelection[0].name !== "") {
                finalName = appSelection[0].name;
            }
            return finalName;
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getLayerName()', ex);
            return '';
        }
    },
    getLayerInfo: function () {
        try {
            var layerObject = {};
            layerObject.gradientColor = {};
            layerObject.patternColor = {};
            var layerColors = [];
            var fillTooltipObj = { fillTooltip: JSXGlobals.FILL };
            var strokeTooltipObj = { strokeTooltip: JSXGlobals.STROKE };
            var isFontAvailableinAIObj = { isFontAvailableinAI: true };

            var appSelection = app.selection;
            if (appSelection) {
                var selection = appSelection;

                layerObject.kind = "";
                if (selection) {
                    if (selection.length === 1 && selection[0] && selection[0].__class__ === "TextFrame") {
                        selection = selection[0].textRange;
                        populateTextOptionsInLayerObject(layerObject, selection, fillTooltipObj, strokeTooltipObj, isFontAvailableinAIObj);
                    } else if (selection.__class__ === "TextRange") {
                        populateTextOptionsInLayerObject(layerObject, selection, fillTooltipObj, strokeTooltipObj, isFontAvailableinAIObj);
                    }
                }

                try {
                    var fillColor = app.activeDocument.defaultFillColor;

                    if (fillColor.typename === 'PatternColor') {
                        layerObject.patternColor.fillPattern = {
                            'name': fillColor.pattern.name,
                            'data': PATTERN.patternColorToData(fillColor)
                        };
                    } else if (fillColor.typename === 'GradientColor') {
                        var pathItem;
                        if (app.activeDocument.selection.length > 0) {
                            fillColor = app.activeDocument.selection[0].fillColor;
                            pathItem = app.activeDocument.selection[0];
                        }
                        layerObject.gradientColor.fillGradient = {
                            'name': fillColor.gradient.name,
                            'data': GRADIENT.gradientColorToData(fillColor, pathItem)
                        };
                    } else {
                        pushUnique(COLOR.solidColorToData(fillColor), fillTooltipObj.fillTooltip, layerColors);
                    }
                } catch (ignore) {}

               try {
                    var strokeColor = app.activeDocument.defaultStrokeColor;

                    if (strokeColor.typename === 'PatternColor') {
                        layerObject.patternColor.strokePattern = {
                            'name': strokeColor.pattern.name,
                            'data': PATTERN.patternColorToData(strokeColor)
                        };
                    } else if (strokeColor.typename === 'GradientColor') {
                        // Call without pathItem for stroke
                        layerObject.gradientColor.strokeGradient = {
                            'name': strokeColor.gradient.name,
                            'data': GRADIENT.gradientColorToData(strokeColor)  // No need to pass pathItem here
                        };
                    } else {
                        pushUnique(COLOR.solidColorToData(strokeColor), strokeTooltipObj.strokeTooltip, layerColors);
                    }
                } catch (ignore) {}


                layerObject.colors = layerColors;

                // Looping through the pageItems is taking too long. Hence commenting it out
                /*var selectedLayer = app.activeDocument.activeLayer;
                var pageItems = selectedLayer.pageItems;
                var numItemsSelected = 0;
                var i;
                for (i = 0; i < pageItems.length; i++) {
                    if (pageItems[i].selected === true) {
                        numItemsSelected++;
                    }
                }*/

                layerObject.name = "None";

                // && pageItems.length !== numItemsSelected
                if (selection[0] && selection[0].name !== "") {
                    layerObject.name = selection[0].name;
                    layerObject.fullName = selection[0].name;
                } else {
                    if (app.activeDocument.activeLayer) {
                        layerObject.name = app.activeDocument.activeLayer.name;
                        layerObject.fullName = app.activeDocument.activeLayer.name;
                    }
                }
                layerObject.name = $._ADBE_LIBS_CORE.shortenString(layerObject.name, false, JSXGlobals.maxNameLength);
                layerObject.selectionExists = (appSelection && appSelection.length > 0);

                layerObject.isAppliedCharStyleSupported = false;
                layerObject.isAppliedParaStyleSupported = false;
                if (layerObject.kind === "LayerKind.TEXT" && isFontAvailableinAIObj.isFontAvailableinAI) {
                    layerObject.isAppliedCharStyleSupported = true;
                    layerObject.isAppliedParaStyleSupported = true;
                }
                //If we happen to have a selection that is not an array but a TextRange object
                //then we are in text edit mode and should report no selection to disable adding it as a graphic
                //but we still wish to allow applying colors to textRange
                if (appSelection && appSelection.typename === 'TextRange') {
                    layerObject.selectionExists = false;
                    // Ensure text selection and not just insertion point
                    if (appSelection.length > 0) {
                        layerObject.textSelectionExists = true;
                    }
                }
            }
            return JSON.stringify(layerObject);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getLayerInfo()', ex);
        }
    },
    getLayerColor: function () {
        try {
            var colorObject = COLOR.solidColorToData(JSXGlobals.colorModifiedByUser);
            return JSON.stringify(colorObject);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getLayerColor()', ex);
        }
    },
    getLayerFontStyle: function (docName) {
        try {
            var docRef = $._ADBE_LIBS_CORE.getDocument(docName);
            if (docRef) {
                docRef.layers[0].textFrames[0].selected = true;
                var layerObject = {};
                var selection = app.selection;
                var selectionFont = selection[0].textRange.characterAttributes.textFont;

                layerObject.text = $._ADBE_LIBS_CORE.shortenString(selection[0].contents);
                layerObject.fontInfo = selectionFont.family + ";" + selectionFont.style + ";" + selection[0].textRange.characterAttributes.size;
                layerObject.textColor = COLOR.solidColorToData(app.activeDocument.defaultFillColor).color;
                return JSON.stringify(layerObject);
            }
            return undefined;
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getLayerFontStyle()', ex);
        }
    },
    getLayerBounds: function () {
        try {
            var selection = app.activeDocument.selection;
            var artboardRect = app.activeDocument.artboards[0].artboardRect;
            var firstItem = UTIL.getRealPosition(artboardRect, selection[0].position);
            var bounds;
            var i;
            var itemBounds, controlBounds;
            for (i = 0; i < selection.length; i++) {
                itemBounds = UTIL.getRealPosition(artboardRect, selection[i].position);
                controlBounds = selection[i].controlBounds;

                itemBounds.width = Math.abs(controlBounds[0] - controlBounds[2]);
                itemBounds.height = Math.abs(controlBounds[1] - controlBounds[3]);
                itemBounds.right = itemBounds.left + itemBounds.width;
                itemBounds.bottom = itemBounds.top + itemBounds.height;

                if (i === 0) {
                    bounds = itemBounds;
                    continue;
                }

                if (itemBounds.top < bounds.top) {
                    bounds.top = itemBounds.top;
                }
                if (itemBounds.left < bounds.left) {
                    bounds.left = itemBounds.left;
                }
                if (itemBounds.right > bounds.right) {
                    bounds.right = itemBounds.right;
                }
                if (itemBounds.bottom > bounds.bottom) {
                    bounds.bottom = itemBounds.bottom;
                }
            }

            if (bounds) {
                bounds.width = bounds.right - bounds.left;
                bounds.height = bounds.bottom - bounds.top;
            }

            var info = {};
            info.name = $._ADBE_LIBS_CORE.shortenString(selection[0].name, false, JSXGlobals.maxNameLength);
            info.bounds = bounds;
            info.artboard = artboardRect;
            info.firstItem = firstItem;

            return JSON.stringify(info);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getLayerBounds()', ex);
        }
    },
    saveAsSVG: function (filePath) {
        try {
            var svgFile = new File(filePath);
            var exportOptions = new ExportOptionsSVG();
            exportOptions.embedRasterImages = true;
            exportOptions.embedAllFonts = true;
            exportOptions.fontSubsetting = SVGFontSubsetting.GLYPHSUSED;
            exportOptions.fontType = SVGFontType.SVGFONT;
            app.activeDocument.exportFile(svgFile, ExportType.SVG, exportOptions);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-saveAsSVG()', ex);
        }
    },
    saveAsPNG: function (filePath, exportFullFile, maxWidth, maxHeight) {
        try {
            var pngFile = new File(filePath);

            var options = getThumbnailOptions(maxWidth, maxHeight);
            if (exportFullFile) {
                app.activeDocument.exportFile(pngFile, ExportType.PNG24, options);
            } else {
                app.activeDocument.exportSelectionAsPNG(pngFile, options);
            }
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-saveAsPNG()', ex);
        }
    },
    setThumbnailExportOptions: function () {
        return undefined;
        //try {
        // TODO: Enable this when exportCloudLibraryAsset() API bugs are fixed
        //app.setThumbnailOptionsForCloudLibrary(getThumbnailOptions());
        //} catch (ex) {}
    },
    saveAssets: function (info, generateSecondaryFormat, dragAssetId) {
        try {
            var fileData = {};
            fileData.files = [];
            /*var svgPath;
            if (generateSecondaryFormat) {
                // ** Save as SVG file **
                svgPath = Folder.temp.fsName + "/" + info.name + ".svg";
                $._ADBE_LIBS_ILST.saveAsSVG(svgPath);
            }*/

            var pngPath = Folder.temp.fsName + "/" + info.name + ".png";
            $._ADBE_LIBS_ILST.saveAsPNG(pngPath, false, info.maxWidth, info.maxHeight);
            fileData.rendition = pngPath;

            //If we couldn't create the PNG file correctly then there is no rendition
            var checkFile = new File(pngPath);
            if (!checkFile.exists) {
                delete fileData.rendition;
            }

            // ** Save as AI file **
            var aiPath = Folder.temp.fsName + "/" + info.name + ".ai";
            var aiFile = new File(aiPath);
            //app.activeDocument.saveAs(aiFile);
            app.activeDocument.exportSelectionAsAi(aiFile);

            checkFile = new File(aiPath);
            if (!checkFile.exists) {
                throw "Could note extract selection.";
            }

            // TODO: Enable this when exportCloudLibraryAsset() API bugs are fixed
            //app.exportCloudLibraryAsset(aiFile, new File(pngPath), dragAssetId);

            fileData.files.push({
                path: aiPath,
                relationship: 'primary'
            });

            /*if (svgPath) {
                fileData.files.push({path: svgPath, relationship: 'alternate'});
            }*/

            fileData.layerName = $._ADBE_LIBS_ILST.getLayerName();

            // TODO: Set layerIds and documentId on fileData if you want it to be returned with the
            // "com.adobe.DesignLibraries.events.ElementCreated" CSEvent that is sent
            // after element creation.

            return JSON.stringify(fileData);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-saveAssets()', ex);
        }
    },
    getTooltipState: function () {
        return app.preferences.getBooleanPreference('showToolTips');
    },
    getIMSUserID: function () {
        return app.userGUID;
    },
    closeDocument: function () {
        return app.activeDocument.close();
    },
    getParaStyleInfo: function () {
        try {
            var styleObj = TEXT.getParagraphStyle();
            return JSON.stringify(styleObj);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getParaStyleInfo()', ex);
        }
    },
    getCharStyleInfo: function () {
        try {
            var styleObj = TEXT.getCharacterStyle();
            return JSON.stringify(styleObj);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getCharStyleInfo()', ex);
        }
    },
    addCharStyleById: function (id) {
        try {
            var styleObj = TEXT.getCharacterStyleById(id);
            return JSON.stringify(styleObj);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getCharStyleInfo()', ex);
        }

    },
    addParaStyleById: function (id) {
        try {
            var styleObj = TEXT.getParagraphStyleById(id);
            return JSON.stringify(styleObj);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getCharStyleInfo()', ex);
        }

    },
    getTextInfo: function () {
        try {
            var textObj = TEXT.getTextInfo();
            return JSON.stringify(textObj);
        } catch (ex) {
            alert(ex);
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getText()', ex);
        }
    },
    getAiFileForText: TEXT.getAiFileForText,
    getUserData: function (mode) {
        try {
            return app.getCCXUserJSONData(mode);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-getCCXUserJSONData()', ex);
        }
    }, 
    saveAssetsPattern: function (info, dragAssetId, patternPathAI, patternPathPNG, patternPathThumbnail) {
        try {
            var fileData = {};
            fileData.files = [];
            fileData.files.push({
                path: patternPathThumbnail,
                relationship: 'rendition'
            }, {
                path: patternPathAI,
                relationship: 'alternate'
            }, {
                path: patternPathPNG,
                relationship: 'primary'
            });
            fileData.rendition = patternPathThumbnail;
            fileData.layerName = $._ADBE_LIBS_ILST.getLayerName();

            return JSON.stringify(fileData);
        } catch (ex) {
            $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-saveAssetsPattern()', ex);
        }
    },
    applyCustomGradient: GRADIENT.applyCustomGradient,
    addGradientAsSwatch: GRADIENT.addGradientAsSwatch,
    isGradientSameName: GRADIENT.isGradientSameName
};

$._ADBE_LIBS_CORE.isDocumentOpen = function (path) {
    if ($.os.indexOf('Windows') > -1) {
        path = path.split('/').join('\\');
    }
    var hasMatch = false;
    try {
        hasMatch = app.getIsFileOpen(path);
    } catch (ex) {
        $._ADBE_LIBS_CORE.writeToLog('ILST.jsx-isDocumentOpen()', ex);
    }
    return JSON.stringify(hasMatch);
};
