package com.parse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public class ParseOperationSet extends HashMap<String, ParseFieldOperation> {
    public boolean isSaveEventually;
    public final String uuid;

    public ParseOperationSet() {
        this(UUID.randomUUID().toString());
    }

    public ParseOperationSet(ParseOperationSet parseOperationSet) {
        super(parseOperationSet);
        this.isSaveEventually = false;
        this.uuid = parseOperationSet.uuid;
        this.isSaveEventually = parseOperationSet.isSaveEventually;
    }

    public ParseOperationSet(String str) {
        this.isSaveEventually = false;
        this.uuid = str;
    }

    public static ParseOperationSet fromRest(JSONObject jSONObject, ParseDecoder parseDecoder) {
        Iterator<String> itKeys = jSONObject.keys();
        String[] strArr = new String[jSONObject.length()];
        int i = 0;
        while (itKeys.hasNext()) {
            strArr[i] = itKeys.next();
            i++;
        }
        JSONObject jSONObject2 = new JSONObject(jSONObject, strArr);
        String str = (String) jSONObject2.remove("__uuid");
        ParseOperationSet parseOperationSet = str == null ? new ParseOperationSet() : new ParseOperationSet(str);
        boolean zOptBoolean = jSONObject2.optBoolean("__isSaveEventually");
        jSONObject2.remove("__isSaveEventually");
        parseOperationSet.isSaveEventually = zOptBoolean;
        Iterator<String> itKeys2 = jSONObject2.keys();
        while (itKeys2.hasNext()) {
            String next = itKeys2.next();
            Object objDecode = parseDecoder.decode(jSONObject2.get(next));
            if (next.equals("ACL")) {
                objDecode = ParseACL.createACLFromJSONObject(jSONObject2.getJSONObject(next), parseDecoder);
            }
            parseOperationSet.put(next, objDecode instanceof ParseFieldOperation ? (ParseFieldOperation) objDecode : new ParseSetOperation(objDecode));
        }
        return parseOperationSet;
    }

    public void mergeFrom(ParseOperationSet parseOperationSet) {
        for (String str : parseOperationSet.keySet()) {
            ParseFieldOperation parseFieldOperationMergeWithPrevious = parseOperationSet.get(str);
            ParseFieldOperation parseFieldOperation = get(str);
            if (parseFieldOperation != null) {
                parseFieldOperationMergeWithPrevious = parseFieldOperation.mergeWithPrevious(parseFieldOperationMergeWithPrevious);
            }
            put(str, parseFieldOperationMergeWithPrevious);
        }
    }

    public JSONObject toRest(ParseEncoder parseEncoder) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        for (String str : keySet()) {
            jSONObject.put(str, ((ParseFieldOperation) get(str)).encode(parseEncoder));
        }
        jSONObject.put("__uuid", this.uuid);
        if (this.isSaveEventually) {
            jSONObject.put("__isSaveEventually", true);
        }
        return jSONObject;
    }
}
