{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "title": "Location",
    "description": "JSON schema for an location",
    "additionalProperties": false,
    "required": [
        "$schema",
        "pid",
        "code",
        "name",
        "library",
        "allow_request"
    ],
    "propertiesOrder": [
        "name",
        "code",
        "is_online",
        "is_pickup",
        "pickup_name",
        "allow_request",
        "send_notification",
        "notification_email",
        "restrict_pickup_to"
    ],
    "properties": {
        "$schema": {
            "title": "Schema",
            "description": "Schema to validate organisation records against.",
            "type": "string",
            "minLength": 9,
            "default": "https://ils.rero.ch/schemas/locations/location-v0.0.1.json"
        },
        "pid": {
            "title": "Location ID",
            "type": "string"
        },
        "code": {
            "title": "Code",
            "description": "Code of the location.",
            "type": "string",
            "form": {
                "validation": {
                    "validators": {
                        "valueAlreadyExists": {}
                    },
                    "messages": {
                        "alreadyTakenMessage": "The code is already taken."
                    }
                }
            }
        },
        "name": {
            "title": "Name",
            "description": "Name of the location.",
            "type": "string",
            "minLength": 1,
            "form": {
                "focus": true
            }
        },
        "is_pickup": {
            "title": "Is pickup location",
            "type": "boolean",
            "default": false,
            "description": "Qualify this location as a pickup location.",
            "form": {
                "validation": {
                    "validators": {
                        "valueAlreadyExists": {
                            "limitToValues": [
                                true
                            ],
                            "filter": "'library.pid:' + model.library.pid"
                        }
                    },
                    "messages": {
                        "alreadyTakenMessage": "There is already one pickup location in this library."
                    }
                }
            }
        },
        "is_online": {
            "title": "Is online location",
            "type": "boolean",
            "default": false,
            "description": "Qualify this location as an online location.",
            "form": {
                "validation": {
                    "validators": {
                        "valueAlreadyExists": {
                            "limitToValues": [
                                true
                            ],
                            "filter": "'library.pid:' + model.library.pid"
                        }
                    },
                    "messages": {
                        "alreadyTakenMessage": "There is already one online location in this library."
                    }
                }
            }
        },
        "pickup_name": {
            "title": "Pickup location name",
            "type": "string",
            "description": "Displayed pickup location name, if different from the location name.",
            "form": {
                "hideExpression": "model.is_pickup === false",
                "expressionProperties": {
                    "templateOptions.required": "model.is_pickup === true"
                },
                "validation": {
                    "validators": {
                        "valueAlreadyExists": {
                            "filter": "'library.pid:' + model.library.pid"
                        }
                    },
                    "messages": {
                        "alreadyTakenMessage": "The pickup location name is already taken."
                    }
                }
            }
        },
        "library": {
            "title": "Library",
            "type": "object",
            "properties": {
                "$ref": {
                    "title": "Library URI",
                    "type": "string",
                    "pattern": "^https://ils.rero.ch/api/libraries/.*?$"
                }
            }
        },
        "allow_request": {
            "title": "Allow request",
            "type": "boolean",
            "description": "If enabled, it allows requests for items linked to this location.",
            "default": true
        },
        "send_notification": {
            "title": "Send notification",
            "description": "Send a email notification when an item of the location has been requested.",
            "type": "boolean",
            "default": false,
            "form": {
                "hideExpression": "model.allow_request === false",
                "expressionProperties": {
                    "templateOptions.required": "model.allow_request === true"
                }
            }
        },
        "notification_email": {
            "title": "Notification email",
            "description": "Specify a generic email address used for notification.",
            "type": "string",
            "format": "email",
            "pattern": "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,})+$",
            "minLength": 6,
            "form": {
                "hideExpression": "model.allow_request === false || model.send_notification === false",
                "expressionProperties": {
                    "templateOptions.required": "model.send_notification === true && model.allow_request === true"
                },
                "templateOptions": {
                    "addonLeft": {
                        "class": "fa fa-at"
                    }
                },
                "validation": {
                    "messages": {
                        "patternMessage": "Email should have at least one <code>@</code> and one <code>.</code>."
                    }
                }
            }
        },
        "restrict_pickup_to": {
            "title": "Restrict pickup to",
            "description": "Select locations where items linked to the current location could be requested.",
            "type": "array",
            "minItems": 1,
            "_uniqueItems": true,
            "items": {
                "type": "object",
                "title": "Location",
                "properties": {
                    "$ref": {
                        "title": "Location URI",
                        "type": "string",
                        "pattern": "^https://ils.rero.ch/api/locations/.*?$",
                        "form": {
                            "remoteOptions": {
                                "type": "locations",
                                "query": "is_pickup:true",
                                "labelField": "pickup_name"
                            }
                        }
                    }
                }
            },
            "form": {
                "wrappers": [
                    "toggle-switch"
                ],
                "templateOptions": {
                    "label": "",
                    "toggle-switch": {
                        "label": "Restrict pickup to",
                        "description": "If enabled, restrict pickup to specific pickup locations."
                    }
                }
            }
        }
    }
}