"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createVersion = createVersion;
const getIncludeListFromItemType_1 = require("./_internal/getIncludeListFromItemType");
const getVersionData_1 = require("./_internal/getVersionData");
const getPrefix_1 = require("./_internal/getPrefix");
const get_prop_1 = require("../objects/get-prop");
const merge_objects_1 = require("../objects/merge-objects");
const object_to_json_blob_1 = require("../resources/object-to-json-blob");
const util_1 = require("../util");
const constants_1 = require("./_internal/constants");
const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
/**
 * Creates and returns a new version of the entity
 * @param entity
 * @param requestOptions
 * @returns
 */
async function createVersion(model, requestOptions, options) {
    const includeList = (0, getIncludeListFromItemType_1.getIncludeListFromItemType)(model);
    // TODO: in the future, we could make the data a separate resource file and reference it here with jsonref or something: { data: "#resources/data.json" }
    const data = (0, getVersionData_1.getVersionData)(model, includeList);
    const name = options === null || options === void 0 ? void 0 : options.name;
    const description = options === null || options === void 0 ? void 0 : options.description;
    const parent = options === null || options === void 0 ? void 0 : options.parentId;
    const id = (0, util_1.createId)();
    const prefix = (0, getPrefix_1.getPrefix)(id);
    const now = Date.now();
    const version = {
        created: now,
        creator: (0, get_prop_1.getProp)(requestOptions, "authentication.username"),
        data,
        description,
        id,
        name,
        parent,
        path: `${prefix}/${constants_1.VERSION_RESOURCE_NAME}`,
        updated: now,
    };
    const versionBlob = (0, object_to_json_blob_1.objectToJsonBlob)(version);
    const properties = (0, merge_objects_1.mergeObjects)(version, {}, constants_1.VERSION_RESOURCE_PROPERTIES);
    await (0, arcgis_rest_portal_1.addItemResource)(Object.assign(Object.assign({}, requestOptions), { id: (0, get_prop_1.getProp)(model, "item.id"), name: constants_1.VERSION_RESOURCE_NAME, owner: (0, get_prop_1.getProp)(model, "item.owner"), params: { properties }, prefix, resource: versionBlob }));
    version.size = versionBlob.size;
    return version;
}
//# sourceMappingURL=createVersion.js.map