/**
 * Copyright (c) 2022 Yansen Zhang
 * wxcomponent is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
**/

package authorizer

import (
	"errors"
	"net/url"

	"gitee.com/yansen_zh/wxcomponent/config"
	"gitee.com/yansen_zh/wxcomponent/utils/request"
)

const (
	apiSetAuthorizerOption = "https://apies.weixin.qq.com/cgi-bin/component/api_set_authorizer_option"
)

// SetAuthorizerOption 设置授权方选项信息
func SetAuthorizerOption(authorizerAppID, optionName, optionValue string) error {
	if authorizerAppID == "" {
		return errors.New("设置授权方选项信息 第三方平台的 appid 不能为空")
	}

	if optionName == "" {
		return errors.New("设置授权方选项信息 选项名称 不能为空")
	}

	if optionValue == "" {
		return errors.New("设置授权方选项信息 设置的选项值 不能为空")
	}

	queryParam := url.Values{}
	queryParam.Set("component_access_token", config.GetAccessToken())

	data := map[string]string{
		"component_appid":  config.GetAppID(),
		"authorizer_appid": authorizerAppID,
		"option_name":      optionName,
		"option_value":     optionValue,
	}

	_, err := request.PostJSON(apiSetAuthorizerOption, &queryParam, data)
	return err
}