"use strict";

const IconTables = require("../../icons/icon-tables.js");
const Strategy = require("../strategy.js");


class PathStrategy extends Strategy {

	constructor(){
		super({
			priority:      1,
			matchesFiles:  true,
			matchesDirs:   true,
			noSetting:     true,
			ignoreVirtual: false,
		});
	}
	
	
	registerResource(resource){
		if(super.registerResource(resource)){
			const disposables = this.resourceEvents.get(resource);
			disposables.add(resource.onDidMove(() => this.check(resource, false)));
			this.resourceEvents.set(resource, disposables);
			return true;
		}
		else return false;
	}
	
	
	matchIcon(resource){
		let icon = null;
		
		if(resource.isDirectory)
			return icon =
				IconTables.matchPath(resource.path, true) ||
				IconTables.matchName(resource.name, true) ||
				null;
		
		else{
			let {name} = resource;
			let path = resource.realPath || resource.path;
			
			let isFiltered = false;
			const filtered = this.filter(name) || name;
			if(filtered !== name){
				isFiltered = true;
				name = filtered;
				path = this.filter(path);
			}
			
			icon =
				IconTables.matchPath(path) ||
				IconTables.matchName(name) ||
				null;
			
			if(isFiltered && (null === icon || icon.priority < 1))
				icon = IconTables.matchName(resource.name);
			
			return icon || null;
		}
	}
	
	
	filter(path){
		return path
			.replace(/~(?:orig|previous)$/, "")
			.replace(/^([^.]*\.[^.]+)\.(?:inc?|dist|tm?pl|te?mp|ti?dy)$/i, "$1");
	}
}


module.exports = new PathStrategy();

@@ -679,31 +679,24 @@
 rectory)%0A%09%09%09
-return 
 icon =%0A%09%09%09%09I
@@ -1272,20 +1272,22 @@
 ame);%0A%09%09
-%09
+%7D
 %0A%09%09
+%0A%09
 %09return 
@@ -1300,20 +1300,16 @@
 %7C null;%0A
-%09%09%7D%0A
 %09%7D%0A%09%0A%09%0A%09

Refactor unused variable assignment (#827)