'use strict';

import path from 'path';
/**
 * find template file path
 * @param  {String}  
 * @return {Class}
 */
export default class extends think.middleware.base {
  /**
   * get path prefix
   * @return {String} []
   */
  getPathPrefix(module){
    let pathPrefix, http = this.http, prefix = '';
    let {root_path, theme, locale} = this.options;
    
    module = module || http.module;
    
    //support locale
    if(locale){
      prefix += `/${locale}`;
    }
    //support theme
    if(theme){
      prefix += `/${theme}`;
    }

    //view root path is defined
    if(root_path){
      pathPrefix = path.normalize(root_path);
      if(think.mode !== think.mode_mini){
        pathPrefix += prefix + '/' + module;
      }
    }else{
      pathPrefix = think.getPath(module, think.dirname.view, prefix);
    }

    return pathPrefix;
  }
  /**
   * run
   * @param  {String} templateFile [template filepath]
   * @return {}              []
   */
  run(options){
    if(!think.isObject(options)){
      options = think.extend({
        templateFile: options
      }, this.config('tpl'));
    }
    this.options = options;

    let templateFile = options.templateFile;
    //is absolute file path
    if(templateFile && path.isAbsolute(templateFile)){
      return templateFile;
    }
    let http = this.http;
    let {file_depr, file_ext} = options;
    let pathPrefix = this.getPathPrefix();
    
    // this.display()
    if (!templateFile) {
      return pathPrefix + '/' + http.controller + file_depr + http.action + file_ext;
    }
    //replace : to /
    templateFile = templateFile.replace(/\:/g, '/');
    // this.display('detail')
    // this.display('index/detail')
    // this.display('admin/index/detail')
    // this.display('admin/index/detail.html')
    let paths = templateFile.split('/');
    let action = paths.pop();
    let controller = paths.pop() || http.controller;
    let module = paths.pop() || http.module;

    if (module !== http.module) {
      pathPrefix = this.getPathPrefix(module);
    }

    templateFile = pathPrefix + '/' + controller + file_depr + action;
    if (action.indexOf('.') === -1) {
      templateFile += file_ext;
    }
    return templateFile;
  }
}
@@ -324,21 +324,19 @@
 theme, l
-ocale
+ang
 %7D = this
@@ -420,21 +420,19 @@
     if(l
-ocale
+ang
 )%7B%0A     
@@ -447,21 +447,19 @@
 += %60/$%7Bl
-ocale
+ang
 %7D%60;%0A    

rename locale to lang in locate template middleware