'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var axios = require('axios');
var tai_auth_auth = require('../auth/auth.js');
require('../../nodejs/crypto.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);

/**
 * 创建 iWiki 文档
 * 支持创建 Markdown、新文档、文件夹等类型，需要提供 PaaS 认证信息
 * @param {object} config 配置信息
 * @param {string} config.prefix API 前缀地址
 * @param {string} [config.spacekey] 空间 key（与 spaceId 二选一）
 * @param {string} [config.spaceId] 空间 ID（与 spacekey 二选一）
 * @param {string} [config.contenttype='MD'] 内容类型，DOC：新文档，MD：Markdown，FOLDER：文件夹
 * @param {string} [config.title] 文档标题
 * @param {string} [config.body] 文档内容
 * @param {string|number} [config.parentid] 父目录 ID
 * @param {object} [config.otherData] 其他额外参数
 * @param {string} config.paasId PaaS 认证 ID
 * @param {string} config.paasToken PaaS 认证 Token
 * @returns {Promise<{code: string, msg: string, data: {id: string, docid: string}, request_id: string}>} 创建结果
 * @example
 * ```ts
 * const result = await createIwikiDoc({
 *   prefix: 'https://iwiki.woa.com/api',
 *   spacekey: 'my-space',
 *   title: '新文档',
 *   body: '# Hello\n内容',
 *   paasId: 'xxx',
 *   paasToken: 'xxx',
 * });
 * ```
 */
function createIwikiDoc(_ref) {
  var prefix = _ref.prefix,
    spacekey = _ref.spacekey,
    spaceId = _ref.spaceId,
    _ref$contenttype = _ref.contenttype,
    contenttype = _ref$contenttype === void 0 ? 'MD' : _ref$contenttype,
    title = _ref.title,
    body = _ref.body,
    parentid = _ref.parentid,
    _ref$otherData = _ref.otherData,
    otherData = _ref$otherData === void 0 ? {} : _ref$otherData,
    paasId = _ref.paasId,
    paasToken = _ref.paasToken;
  var headers = tai_auth_auth.getAuthOfTai({
    paasId: paasId,
    paasToken: paasToken
  });
  return new Promise(function (resolve, reject) {
    axios__default["default"]({
      headers: headers,
      method: 'POST',
      url: "".concat(prefix, "/v2/doc/create"),
      data: Object.assign({
        spaceId: spaceId,
        spacekey: spacekey,
        parentid: parentid,
        contenttype: contenttype,
        title: title,
        body: body
      }, otherData || {})
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}

exports.createIwikiDoc = createIwikiDoc;
