var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dingtalk_client_exports = {};
__export(dingtalk_client_exports, {
  DingtalkClient: () => DingtalkClient
});
module.exports = __toCommonJS(dingtalk_client_exports);
var import_contact_1_0 = __toESM(require("@alicloud/dingtalk/contact_1_0"));
var import_oauth2_1_0 = __toESM(require("@alicloud/dingtalk/oauth2_1_0"));
var import_openapi_client = require("@alicloud/openapi-client");
var import_tea_util = __toESM(require("@alicloud/tea-util"));
class DingtalkClient {
  constructor(config) {
    this.config = config;
    this.ctx = config.ctx;
  }
  createAuthClient() {
    const config = new import_openapi_client.Config({});
    config.protocol = "https";
    config.regionId = "central";
    return new import_oauth2_1_0.default(config);
  }
  createContactClient() {
    const config = new import_openapi_client.Config({});
    config.protocol = "https";
    config.regionId = "central";
    return new import_contact_1_0.default(config);
  }
  async getAccessToken(code) {
    var _a;
    const client = this.createAuthClient();
    const getUserTokenRequest = new import_oauth2_1_0.GetUserTokenRequest({
      clientId: this.config.clientId,
      clientSecret: this.config.clientSecret,
      code,
      grantType: "authorization_code"
    });
    try {
      const response = await client.getUserToken(getUserTokenRequest);
      return ((_a = response == null ? void 0 : response.body) == null ? void 0 : _a.accessToken) || "";
    } catch (err) {
      if (!import_tea_util.default.empty(err.code) && !import_tea_util.default.empty(err.message)) {
        this.ctx.logger.warn(`DingTalk get accessToken error code:${err.code}, message:${err.message}`);
      }
      return "";
    }
  }
  async getUserInfo(accessToken) {
    const client = this.createContactClient();
    const getUserHeaders = new import_contact_1_0.GetUserHeaders({});
    getUserHeaders.xAcsDingtalkAccessToken = accessToken;
    try {
      const response = await client.getUserWithOptions("me", getUserHeaders, new import_tea_util.RuntimeOptions({}));
      return (response == null ? void 0 : response.body) || {};
    } catch (err) {
      if (!import_tea_util.default.empty(err.code) && !import_tea_util.default.empty(err.message)) {
        this.ctx.logger.warn(`DingTalk get userInfo error code:${err.code}, message:${err.message}`);
      }
      return {};
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DingtalkClient
});
