var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { Language, LegalBasis } from '@signatu/common-lib';
import { withStyles } from '@signatu/styles';
import { PolicyDataProcessing, TextContent } from '@signatu/policy';
import PropTypes from 'prop-types';
import React from 'react';
import { ConsentTitle } from '../lib/consentTitle';
import { DataProcessingConsent } from './dataProcessingConsent';
var DEFAULT_DATAPROCESSING_API = "https://api.signatu.com/dataprocessing/v0";
var styles = function (theme) { return ({
    root: {
        marginTop: "2em",
    },
    consentContainer: {
        marginBottom: "1.5em"
    }
}); };
var DataProcessingGroup = /** @class */ (function (_super) {
    __extends(DataProcessingGroup, _super);
    function DataProcessingGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {};
        _this.componentIsMounted = false;
        _this.endPoint = "";
        return _this;
    }
    DataProcessingGroup.prototype.componentWillMount = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (this.props.groupId) {
                    this.loadGroup(this.props.groupId);
                }
                return [2 /*return*/];
            });
        });
    };
    DataProcessingGroup.prototype.componentWillReceiveProps = function (newProps) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (newProps.groupId !== this.props.groupId) {
                    this.loadGroup(newProps.groupId);
                }
                return [2 /*return*/];
            });
        });
    };
    DataProcessingGroup.prototype.componentDidMount = function () {
        this.componentIsMounted = true;
    };
    DataProcessingGroup.prototype.componentWillUnmount = function () {
        this.componentIsMounted = false;
    };
    DataProcessingGroup.prototype.loadGroup = function (groupId) {
        return __awaiter(this, void 0, void 0, function () {
            var base, response, group;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        base = this.props.config && this.props.config.dataprocessingAPI || DEFAULT_DATAPROCESSING_API;
                        this.endPoint = base.replace(/\/*$/, "") + ("/dpgroups/" + groupId);
                        return [4 /*yield*/, fetch(this.endPoint, {
                                method: 'GET',
                                headers: {
                                    Authorization: "Bearer " + this.props.config.accessToken
                                }
                            })];
                    case 1:
                        response = _a.sent();
                        return [4 /*yield*/, response.json()];
                    case 2:
                        group = _a.sent();
                        if (!(group && group.specs)) {
                            return [2 /*return*/];
                        }
                        /** Make the specs PolicyDataProcessing objects */
                        group.specs = group.specs.map(function (s) {
                            var dp = new PolicyDataProcessing(s.action, s.data, s.purpose, s.location, s.sensitive, s.consentTarget, s.legalBasis);
                            /** This will be used as the scope */
                            dp.elementId = s.id;
                            /** Add all texts as content */
                            (s.texts || []).forEach(function (t) { return dp.addContent(new TextContent(t.value, new Language(t.language))); });
                            return dp;
                        });
                        if (this.componentIsMounted) {
                            this.setState({ group: group });
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    DataProcessingGroup.prototype.render = function () {
        var _this = this;
        var _a = this.props, config = _a.config, subject = _a.subject, issuer = _a.issuer, type = _a.type, delegate = _a.delegate, classes = _a.classes, language = _a.language;
        var group = this.state.group;
        if (!group) {
            return React.createElement("div", { "data-type": "DataProcessingGroup" },
                "No group: ",
                this.props.groupId);
        }
        return (React.createElement("div", { className: classes.root, "data-type": "DataProcessingGroup" },
            React.createElement(ConsentTitle, { title: group.name, subtitle: React.createElement("div", null, group.description) }),
            group.specs.map(function (s, idx) { return React.createElement("div", { className: classes.consentContainer, key: idx },
                React.createElement(DataProcessingConsent
                /** Set all common props */
                , { 
                    /** Set all common props */
                    config: config, subject: subject, issuer: issuer, delegate: delegate, type: type, language: language, 
                    /** Overrides */
                    scope: s.elementId, target: _this.endPoint, source: document.location && document.location.href || undefined, title: undefined, subtitle: undefined, optOut: s.legalBasis === LegalBasis.LEGITIMATE_INTEREST, dataprocessing: s })); })));
    };
    /** PropTypes for non-Typescript environments */
    DataProcessingGroup.propTypes = {
        config: PropTypes.object.isRequired,
        subject: PropTypes.string.isRequired,
        issuer: PropTypes.string.isRequired,
        groupId: PropTypes.string.isRequired,
        type: PropTypes.string,
        delegate: PropTypes.string,
        language: PropTypes.string,
        onAction: PropTypes.func,
        classes: PropTypes.object
    };
    return DataProcessingGroup;
}(React.Component));
var styled = withStyles(styles)(DataProcessingGroup);
export { styled as DataProcessingGroup };
//# sourceMappingURL=dataProcessingGroup.js.map