"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var errors = _interopRequireWildcard(require("../errors.js"), true);
var _helper = require("./helper.js");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// Build PostPolicy object that can be signed by presignedPostPolicy

class PostPolicy {
  policy = {
    conditions: []
  };
  formData = {};

  // set expiration date
  setExpires(date) {
    if (!date) {
      throw new errors.InvalidDateError('Invalid date: cannot be null');
    }
    this.policy.expiration = date.toISOString();
  }

  // set object name
  setKey(objectName) {
    if (!(0, _helper.isValidObjectName)(objectName)) {
      throw new errors.InvalidObjectNameError(`Invalid object name : ${objectName}`);
    }
    this.policy.conditions.push(['eq', '$key', objectName]);
    this.formData.key = objectName;
  }

  // set object name prefix, i.e policy allows any keys with this prefix
  setKeyStartsWith(prefix) {
    if (!(0, _helper.isValidPrefix)(prefix)) {
      throw new errors.InvalidPrefixError(`Invalid prefix : ${prefix}`);
    }
    this.policy.conditions.push(['starts-with', '$key', prefix]);
    this.formData.key = prefix;
  }

  // set bucket name
  setBucket(bucketName) {
    if (!(0, _helper.isValidBucketName)(bucketName)) {
      throw new errors.InvalidBucketNameError(`Invalid bucket name : ${bucketName}`);
    }
    this.policy.conditions.push(['eq', '$bucket', bucketName]);
    this.formData.bucket = bucketName;
  }

  // set Content-Type
  setContentType(type) {
    if (!type) {
      throw new Error('content-type cannot be null');
    }
    this.policy.conditions.push(['eq', '$Content-Type', type]);
    this.formData['Content-Type'] = type;
  }

  // set Content-Type prefix, i.e image/ allows any image
  setContentTypeStartsWith(prefix) {
    if (!prefix) {
      throw new Error('content-type cannot be null');
    }
    this.policy.conditions.push(['starts-with', '$Content-Type', prefix]);
    this.formData['Content-Type'] = prefix;
  }

  // set Content-Disposition
  setContentDisposition(value) {
    if (!value) {
      throw new Error('content-disposition cannot be null');
    }
    this.policy.conditions.push(['eq', '$Content-Disposition', value]);
    this.formData['Content-Disposition'] = value;
  }

  // set minimum/maximum length of what Content-Length can be.
  setContentLengthRange(min, max) {
    if (min > max) {
      throw new Error('min cannot be more than max');
    }
    if (min < 0) {
      throw new Error('min should be > 0');
    }
    if (max < 0) {
      throw new Error('max should be > 0');
    }
    this.policy.conditions.push(['content-length-range', min, max]);
  }

  // set user defined metadata
  setUserMetaData(metaData) {
    if (!(0, _helper.isObject)(metaData)) {
      throw new TypeError('metadata should be of type "object"');
    }
    Object.entries(metaData).forEach(([key, value]) => {
      const amzMetaDataKey = `x-amz-meta-${key}`;
      this.policy.conditions.push(['eq', `$${amzMetaDataKey}`, value]);
      this.formData[amzMetaDataKey] = value.toString();
    });
  }
}
exports.PostPolicy = PostPolicy;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10440 characters]