import axios from 'axios';
import { getAuthOfTai } from '../auth/auth.mjs';
import '../../nodejs/crypto.mjs';

/**
 * 更新 iwiki 文档内容
 * 使用 POST /v2/doc/save 接口，通过 force: true 强制保存
 * @example
 * ```ts
 * await updateIwikiDoc({
 *   prefix: 'https://api.iwiki.woa.com',
 *   id: 12345,
 *   spacekey: 'my-space',
 *   title: '我的文档',
 *   body: '# Hello\n\n更新内容',
 *   contenttype: 'MD',
 *   paasId: 'xxx',
 *   paasToken: 'yyy',
 * });
 * ```
 */
function updateIwikiDoc(_ref) {
  var prefix = _ref.prefix,
    id = _ref.id,
    spacekey = _ref.spacekey,
    title = _ref.title,
    body = _ref.body,
    _ref$contenttype = _ref.contenttype,
    contenttype = _ref$contenttype === void 0 ? 'MD' : _ref$contenttype,
    _ref$otherData = _ref.otherData,
    otherData = _ref$otherData === void 0 ? {} : _ref$otherData,
    paasId = _ref.paasId,
    paasToken = _ref.paasToken,
    _ref$force = _ref.force,
    force = _ref$force === void 0 ? true : _ref$force;
  var headers = getAuthOfTai({
    paasId: paasId,
    paasToken: paasToken
  });
  return new Promise(function (resolve, reject) {
    axios({
      headers: headers,
      method: 'POST',
      url: "".concat(prefix, "/v2/doc/save"),
      data: Object.assign({
        id: id,
        spacekey: spacekey,
        title: title,
        body: body,
        contenttype: contenttype,
        force: force
      }, otherData || {})
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}

export { updateIwikiDoc };
