flowchart TD
    subgraph Roles["Role on the collection (direct or through an organisation, whichever is higher)"]
        R[read] --> W[write] --> M[manage] --> O[owner]
    end

    View{"Open a collection"} -->|"private and no role"| Hidden["Not found"]
    View -->|"has a role, or not private"| Visible["Can view. Without a role: read only, and can clone"]

    subgraph Write["write and above"]
        W1["Open or import items from the source"]
        W2["Add a manifest"]
        W3["Drag manuscripts and collections in"]
        W4["Move own items up one level"]
    end

    subgraph Manage["manage and above"]
        M1["Edit title, description, colour"]
        M2["Change visibility (applies only to contents you own or manage)"]
        M3["Add, remove, change collaborators"]
        M4["Assign an organisation"]
        M5["Remove other people's items from it"]
        M6["Open Settings from the folder menu, including sub-collections"]
    end

    subgraph Owner["owner only"]
        O1["Delete the collection"]
        O2["Transfer ownership"]
        O3["Move the collection into another"]
    end

    W --> Write
    M --> Manage
    O --> Owner

    M3 --> RoleChange{"Change a collaborator's role"}
    RoleChange --> Apply["Ask: apply to everything inside as well? Same question for upgrades and downgrades. The owner's own items are never changed"]
    M3 --> RemoveUser["Remove from the collection and everything inside, except items they own"]

    O1 --> Del{"Collection has a parent?"}
    Del -->|yes| DelUp["Contents move up to the parent. Access stays"]
    Del -->|no| DelRoot["Contents move to the dashboard. Ask: keep or remove access for people who only had it through this collection"]

    O3 --> Drop{"Drop onto a collection"}
    Drop --> Target{"You have write or higher on the target, and it is not inside the moved collection?"}
    Target -->|no| Refuse["Refused"]
    Target -->|yes| Choose{"Merge or put inside"}
    Choose -->|merge| Merge["Items move over and the emptied collection is deleted"]
    Choose -->|"put inside"| Nest["Becomes a sub-collection"]
    Merge --> Inherit["Target's people gain access. Old parent access is kept as direct access"]
    Nest --> Inherit

    W4 --> UpC{"Moving a sub-collection up one level"}
    M5 --> UpC
    UpC --> UpOwner{"You own the sub-collection?"}
    UpOwner -->|yes| UpGrand{"Needs write or higher on the grandparent, if there is one"}
    UpGrand --> UpAsk["Ask: keep or remove access from the old parent"]
    UpOwner -->|no| UpMgr{"You manage the parent?"}
    UpMgr -->|yes| UpRemove["Warning, then it goes to its owner's top level and all inherited access ends"]
    UpMgr -->|no| UpNo["Refused: only the owner can move a collection"]
flowchart TD
    subgraph Roles["Role on the manuscript"]
        R[read] --> W[write] --> M[manage] --> O[owner]
    end

    Start([Move a manuscript]) --> Where{Where to?}

    Where -->|"Into a collection"| In{"Manuscript role?"}
    In -->|"read / write"| InNo["Refused: only an owner or manager can move it"]
    In -->|owner| InOwn{"You have write or higher on the collection?"}
    In -->|manager| InMgr{"You have write or higher on the collection?"}
    InOwn -->|no| InDeny["Refused: no permission to add"]
    InOwn -->|yes| Prot
    InMgr -->|no| InDeny
    InMgr -->|yes| OwnerCheck{"Does the manuscript's owner have write or higher on the collection?"}
    OwnerCheck -->|no| OwnerDeny["Refused: the owner needs edit access to the collection"]
    OwnerCheck -->|yes| Prot
    Prot{"Protected or demo manuscript, or already in that collection?"}
    Prot -->|yes| InDeny
    Prot -->|no| Notice["If people gain access, a confirmation lists who"]
    Notice --> Inherit["Collection members get access, marked as inherited. Lower roles are raised"]

    Where -->|"Up one level out of its collection"| Up{"Your role on the current collection?"}
    Up -->|"read"| UpNo["Refused: no permission"]
    Up -->|"write / manage / owner"| Others{"Manuscript's owner is someone else AND you manage the collection?"}
    Others -->|yes| Remove["Warning, then it goes back to its owner's top level. All access given through the collection ends, including yours"]
    Others -->|no| MRole{"Your role on the manuscript?"}
    MRole -->|"read / write"| UpNo2["Refused: only an owner or manager can move it out"]
    MRole -->|"owner / manager"| HasGrand{"Collection is inside another collection?"}
    HasGrand -->|no| Root["Moves to the owner's top level"]
    HasGrand -->|yes| GrandPerm{"You have write or higher on the grandparent? (managers: the owner too)"}
    GrandPerm -->|no| UpNo3["Refused: no permission to add to the parent collection"]
    GrandPerm -->|yes| Grand["Moves into the grandparent"]
    Root --> Keep{"Access came through the collection?"}
    Grand --> Keep
    Keep -->|yes| Choice["Ask: keep access or remove their access"]
    Keep -->|no| Done([Moved])
    Choice --> Done

    Where -->|"Dropped on another manuscript"| Stack{"You own both, in the same collection?"}
    Stack -->|yes| NewColl["New collection (sub-collection if inside one). You become its owner"]
    Stack -->|no| StackNo["Refused"]