class MenuControl extends DatasetControl {
	constructor() {
		super({
			keymapper: info => info ? info.id : "",
			prefix:"menu",
			prefixName:"메뉴"
		});
		
		this.urls.load = this.url("/tree.do");
		delete this.urls.getInfo;
		this.menus = [];
	}
	
	setData(obj) {
		let list = [];
		let asList = array => {
			if (!array || array.length < 1) return;
			
			for (var i = 0; i < array.length; ++i) {
				let menu = array[i];
				list.push(menu);
				asList(menu.children);
			}
		};
		asList(this.menus = Array.isArray(obj) ? obj : obj.menus);
		super.setData(list);
	}
	
	move(parentID, menuID) {
		json.post({
			url:wctx.url("/menu/move.do"),
			data:{
				parentID:parentID,
				menuIDs:menuID
			},
			success:resp => {
				this._load();
				this.onMenusChanged();
			}
		});
	}

	reorder(menuIDs) {
		json.post({
			url:wctx.url("/menu/reorder.do"),
			data:{
				menuIDs:Array.isArray(menuIDs) ? menuIDs.join(",") : menuIDs
			},
			success:resp => {
				this._load();
				this.onMenusChanged();
			}
		});
	}
	
	onSave(resp) {
		super.onSave(resp);
		this.onMenusChanged();
	}
	
	onRemove(selected, resp) {
		super.onRemove(selected, resp);
		this.onMenusChanged();
	}
	
	onMenusChanged() {}
	
	async getUserMenus() {
		return new Promise((resolve, reject) => {
			json.get({
				url:this.url("/userMenus.do"),
				data:{},
				success:resp => resolve(resp.userMenus)
			});
		});
	}
}