<NME> action-simulate-keynote.cocoascript
<BEF> @import 'inventory/classes/state.cocoascript'

// generate code for layers
var runSimulateKeynote = function() {
	StatisticsInventory.setStatus(Status.simulateID())
	FramerInventory.setUserSelection(currentSelection)
	var doc = context.document
	var selection = context.selection
	
	// if (isTestingEnvironment != nil && isTestingEnvironment) {
// 		if (showLog) { log("Prepare custom selection for keynote tests") }
// 		selection = TestingEnvironment.getTestingSelection()
	if (userDefaults.exportFramerFolder == "") {
			selectFolder()
			return
	}

	var artboardsScope = FramerInventory.returnArtboardsScope()
	var duplicatedArtboard = FramerInventory.returnDuplicatedArtboards(artboardsScope)
	if (duplicatedArtboard != nil) {
		if (showLog) { log("Warning: duplicated artboards names are found") }
		selectDuplicatedArtboards([duplicatedArtboard name])
		return
	}

	var sortedSelection = FramerInventory.organiseSelection(returnExportableLayersForSelectedScope(artboardsScope))
	if (sortedSelection == nil) {
		[currentDocument showMessage:ViewInventory.exportPrototypesSelectionError()]
		return
	}

	

	var sortedSelection = FramerInventory.organiseSelection([currentPage exportableLayers])
	
	for (var i = 0; i < [sortedSelection count]; i++) {
		var currentLayer = [sortedSelection objectAtIndex: i]
		var currentLayer = [sortedSelection objectAtIndex: i]
		var layerName = [currentLayer name]
		var layer = new Layer(layerName)

		var hasInitialState = false
		var hasCurrentState = false


		for (var j = 0; j < artboardsMaps.count(); j++) {
			var currentMap = [artboardsMaps objectAtIndex:j]

			var hasPrevState = hasCurrentState
			var prevMapLayer = mapStateLayer

			var mapStateLayer = [currentMap objectForKey:layerName]

			if (mapStateLayer != nil) {
				hasCurrentState = true

				if (j == 0) { hasInitialState = true }
				else if (!hasInitialState) {
					var fakeFirstState = new State(mapStateLayer)
					fakeFirstState.createHiddenState([artboardsNames firstObject])
					layer.addState(fakeFirstState)
					hasInitialState = true
				}

				layer.composeState(mapStateLayer)
			}
			else {
				hasCurrentState = false

				if (hasPrevState) {
					var fakeCloseState = new State(prevMapLayer)
					fakeCloseState.createHiddenState([artboardsNames objectAtIndex:j])
					layer.addState(fakeCloseState)
				}
			}
		}

		layers.push(layer)
	}

	pasteboardContent += DescribeInventory.generateRetinaVariable() + ln()
	pasteboardContent += DescribeInventory.generatePhoneModel() + dln()
	pasteboardContent += DescribeInventory.sectionView()

	for (var i = 0; i < layers.length; i++) { pasteboardContent += layers[i].printLayer() }

	pasteboardContent += ln()
	pasteboardContent += DescribeInventory.sectionModel()
	pasteboardContent += DescribeInventory.generateCycleButton()

	var cycleStates = FramerInventory.getUsedStatesNames())
	pasteboardContent += DescribeInventory.generateCycleStates(cycleStates)

	pasteboardContent += DescribeInventory.generateLayers(sortedSelection)
	pasteboardContent += DescribeInventory.generateCycler() + ln()
	pasteboardContent += DescribeInventory.sectionCustom()

	if (userDefaults.exportFramerFolder != "" && [[NSFileManager defaultManager] fileExistsAtPath:userDefaults.exportFramerFolder + "/app.coffee"]) {
			var currentString = [NSString stringWithContentsOfFile:userDefaults.exportFramerFolder + "/app.coffee" encoding:NSUTF8StringEncoding error:nil]
			var currentPrototypeParts = currentString.split(DescribeInventory.sectionCustom())

	
	if (!isTestingEnvironment) {
		[[NSPasteboard generalPasteboard] clearContents];
		[[NSPasteboard generalPasteboard] setString:pasteboardContent  forType:NSStringPboardType];
		[doc showMessage:"Done"]
	}

	[[NSPasteboard generalPasteboard] setString:pasteboardContent forType:NSStringPboardType];
	DemoInventory.actionUsed()
	FramerInventory.showDoneMessage()

	StatisticsInventory.setStatus(Status.waitingID())
	return pasteboardContent
}






var returnExportableLayersForSelectedScope = function(selectedScope) {
	var currentPage = FramerInventory.getCurrentPage()
	var pageExportables = [currentPage exportableLayers]

	var scopeExportables = NSMutableArray.new()

	for (var i = 0; i < pageExportables.count(); i++) {
			var currentLayer = [pageExportables objectAtIndex: i]

			if (exportableInScope(currentLayer, selectedScope)) {
				scopeExportables.addObject(currentLayer)
				// log("+1 exportable")
				// log(currentLayer)
				// log("---")
			}

	}

	// log("ALL: ")
	// log([[currentPage exportableLayers] count])
	//
	// log("SEL: ")
	// log([scopeExportables count])

	// return [currentPage exportableLayers]
	return scopeExportables
}


var exportableInScope = function(layer, artboardsScope) {
	var currentParentArtboard = TypeInventory.findParentArtboard(layer)

	for (var i = 0; i < artboardsScope.count(); i++) {
		var currentScopeArtboard = [artboardsScope objectAtIndex:i]
		if (FramerInventory.sameArtboards(currentParentArtboard, currentScopeArtboard)) { return true }
	}

	return false
}

<MSG> Show helpers for zero states

When nothing is selected (for Replicate and Generate) and when nothing
is exportable (for Simulate), we should:
- show a tip what to do (select or make exportable)
- clear PasteBoard and do not export our code

<DFF> @@ -7,6 +7,8 @@ var runSimulateKeynote = function(context, isTestingEnvironment) {
 	var doc = context.document
 	var selection = context.selection
 	
+	[[NSPasteboard generalPasteboard] clearContents]
+	
 	// if (isTestingEnvironment != nil && isTestingEnvironment) {
 // 		if (showLog) { log("Prepare custom selection for keynote tests") }
 // 		selection = TestingEnvironment.getTestingSelection()
@@ -32,6 +34,10 @@ var runSimulateKeynote = function(context, isTestingEnvironment) {
 	
 
 	var sortedSelection = FramerInventory.organiseSelection([currentPage exportableLayers])
+	if (sortedSelection == nil) {
+		[doc showMessage:"Nothing to simulate. Please, make some layers exportable"]
+		return
+	}
 	
 	for (var i = 0; i < [sortedSelection count]; i++) {
 		var currentLayer = [sortedSelection objectAtIndex: i]
@@ -101,7 +107,6 @@ var runSimulateKeynote = function(context, isTestingEnvironment) {
 
 	
 	if (!isTestingEnvironment) {
-		[[NSPasteboard generalPasteboard] clearContents];
 		[[NSPasteboard generalPasteboard] setString:pasteboardContent  forType:NSStringPboardType];
 		[doc showMessage:"Done"]
 	}

Show helpers for zero states