/**
 * Copyright (c) 2016-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import * as Code from './code' ; import * as Either from './either' ; import * as Error from './error' ; import * as File from './file' ; import * as FileWriter from './file-writer' ; import * as List from './list' ; import * as Maybe from './maybe' ; import * as ObjCRenderer from './objc-renderer' ; import * as path from 'path' ; function fileRequest ( containingFolderPath , fileName , contents ) { return Maybe . mbind ( function ( fileContents ) : FileWriter . Request | null { return FileWriter . Request ( File . getAbsoluteFilePath ( path . join ( File . getAbsolutePathString ( containingFolderPath ) , fileName ) ) , fileContents ) ; } , contents ) ; } function fileNameIncludingExtension ( file , extension ) { return file . name + '.' + extension ; } function requestsMaybeContainingRequest ( existingRequests , request ) { return Maybe . match ( function ( fileRequest ) : List . List < FileWriter . Request > { return List . cons ( fileRequest , existingRequests ) ; } , function ( ) { return existingRequests ; } , request ) ; } function implementationFileExtensionForFileType ( fileType ) { switch ( fileType ) { case Code . FileType . ObjectiveC : return 'm' ; case Code . FileType . ObjectiveCPlusPlus : return 'mm' ; } } export function fileCreationRequest ( containingFolderPath , file , renderHeader , renderImpl ) { const headerContents = renderHeader ? ObjCRenderer . renderHeader ( file ) : null ; const headerRequest = fileRequest ( containingFolderPath , fileNameIncludingExtension ( file , 'h' ) , headerContents ) ; const implementationContents = renderImpl ? ObjCRenderer . renderImplementation ( file ) : null ; const implementationRequest = fileRequest ( containingFolderPath , fileNameIncludingExtension ( file , implementationFileExtensionForFileType ( file . type ) ) , implementationContents ) ; const baseRequests = List . of < FileWriter . Request > ( ) ; const requests = requestsMaybeContainingRequest ( requestsMaybeContainingRequest ( baseRequests , headerRequest ) , implementationRequest ) ; const writeRequest = { name : file . name , requests : List . reverse ( requests ) } ; return Either . Right < Error . Error , FileWriter . FileWriteRequest > ( writeRequest ) ; }