// Generated by Create API
// https://github.com/CreateAPI/CreateAPI

import Foundation

public struct MobileDiscoverSchema: Codable, Equatable {
    public var page: Int
    public var sections: [Section]

    public struct Section: Codable, Equatable {
        public var object: Object?
        public var anyJSON: AnyJSON?

        public enum Object: Codable, Equatable {
            case playlistSectionSchema(PlaylistSectionSchema)
            case playlistListSectionSchema(PlaylistListSectionSchema)
            case styleListSectionSchema(StyleListSectionSchema)

            public init(from decoder: Decoder) throws {

                struct Discriminator: Decodable {
                    let sectionType: String
                }

                let container = try decoder.singleValueContainer()
                let discriminatorValue = try container.decode(Discriminator.self).sectionType

                switch discriminatorValue {
                case "playlist": self = .playlistSectionSchema(try container.decode(PlaylistSectionSchema.self))
                case "playlist_list": self = .playlistListSectionSchema(try container.decode(PlaylistListSectionSchema.self))
                case "style_list": self = .styleListSectionSchema(try container.decode(StyleListSectionSchema.self))

                default:
                    throw DecodingError.dataCorruptedError(
                        in: container,
                        debugDescription: "Discriminator value '\(discriminatorValue)' does not match any expected values (playlist, playlist_list, style_list)."
                    )
                }
            }

            public func encode(to encoder: Encoder) throws {
                var container = encoder.singleValueContainer()
                switch self {
                case .playlistSectionSchema(let value): try container.encode(value)
                case .playlistListSectionSchema(let value): try container.encode(value)
                case .styleListSectionSchema(let value): try container.encode(value)
                }
            }
        }

        public init(object: Object? = nil, anyJSON: AnyJSON? = nil) {
            self.object = object
            self.anyJSON = anyJSON
        }

        public init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            self.object = try? container.decode(Object.self)
            self.anyJSON = try? container.decode(AnyJSON.self)
        }

        public func encode(to encoder: Encoder) throws {
            var container = encoder.singleValueContainer()
            if let value = object { try container.encode(value) }
            if let value = anyJSON { try container.encode(value) }
        }
    }

    public init(page: Int, sections: [Section]) {
        self.page = page
        self.sections = sections
    }
}
