;(function () {
	var inner = window.OACustomer

	//命令
	var Commands = {
		//简单命令
		Save: function () {
			wps.Application.ActiveDocument.Save()
		},

		SaveAndClose: function () {
			var doc = wps.Application.ActiveDocument
			doc.Save()
			doc.Close()
		},

		Close: function () {
			var doc = wps.Application.ActiveDocument
			doc.Close(-2)
		},
	}

	//组件
	var Components = {
		SwitchTraceView: {
			getLabel: function () {
				var view = wps.Application.ActiveDocument.ActiveWindow.View
				return view.ShowRevisionsAndComments ? '隐藏痕迹' : '显示痕迹'
			},
			getIcon: function () {
				return '../img/components/SwitchTraceView.png'
			},
			onAction: function () {
				var view = wps.Application.ActiveDocument.ActiveWindow.View
				view.ShowRevisionsAndComments = !view.ShowRevisionsAndComments
			},
		},
	}

	//变量
	var _cacheBtns = {}
	var _initBtn = false

	function buildBtns(type) {
		var btns = []
		var source = inner.getCustomerData(type, 'btns') || []
		if (source.length > 0)
			source.forEach((item) => {
				if (item.componentId) {
					if (!Components.hasOwnProperty(item.componentId)) {
						loginfo('未发现自定义按钮' + item.componentId)
					}
					btns[item.order] = Object.assign(
						buildCmdBtn({}),
						item,
						Components[item.componentId]
					)
				} else if (item.cmd) {
					btns[item.order] = buildCmdBtn(item)
				}
			})

		return btns
	}

	function buildCmdBtn(data) {
		return {
			getIcon: function () {
				return !!data.icon ? '../' + data.icon : '../img/unknow.png'
			},
			getLabel: function () {
				return data.title || '未知'
			},
			onAction: function () {
				!!Commands[data.cmd] && Commands[data.cmd]()
			},
		}
	}

	function getCustomerBtn(type, orderid) {
		if (!_initBtn) {
			_initBtn = true
			_cacheBtns[type] = buildBtns(type)
		}
		return _cacheBtns[type][orderid]
	}

	window.OACustomer = Object.assign({}, inner, {
		getCustomerBtn,
	})
})()
